0

中央に画像があり、右隅にボタンがあるヘッダーを作成しようとしています。

この投稿を見つけて、iOS のようなテキストとボタンを使用して Android でヘッダーを作成する方法と、次のチュートリアルhttp://www.londatiga.net/it/how-to-create-custom-window-title-in-android/、画像を中央に配置して背景を設定することができました。ただし、何をしてもボタンが右隅に表示されません。

背景でマスクされているのか、画面から外れているのかを確認しようとしましたが、問題の理由を見つけることができませんでした.

私のXML

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"> 
        <ImageView
            android:id="@+id/Header"
            android:src="@drawable/header"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#ff000000"
            android:layout_marginTop="5dp"
            android:gravity="center_horizontal"/>

                <Button
            android:id="@+id/lockButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5.0dip"        
            android:text="TEST"
            />


    </LinearLayout>
4

3 に答える 3

1

なぜだめですかrelative

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/img_vs_navbar" >

<ImageView
    android:id="@+id/img_back"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:src="@drawable/btn_setting_back" />

</RelativeLayout>

android:layout_centerInParentこんな感じで中央に寄せることができます。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/img_vs_navbar" >

    <ImageView
        android:id="@+id/img_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/btn_setting_back" />

</RelativeLayout>

またはalignParentRight右に置くために使用します

編集:

これは、1 つの xml で 2 つの imageview を使用するためのものです。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/img_vs_navbar" >

    <ImageView
        android:id="@+id/img_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:src="@drawable/btn_setting_back" />

    <ImageView
        android:id="@+id/img_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/btn_setting_back" />

</RelativeLayout>
于 2013-01-29T17:27:45.793 に答える
1

これを試して :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/Header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="#ff000000"
        android:gravity="center_horizontal"
        android:src="@drawable/header" />

    <Button
        android:id="@+id/lockButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="5.0dip"
        android:text="TEST" />

</RelativeLayout>
于 2013-01-29T17:38:30.043 に答える
0

ImageView には fill_parent 幅があるため、weight 属性を追加する必要があります。

android:layout_weight="1"

行全体にまたがらせたい場合は、LinearLayout を RelativeLayout に変更することができます。

于 2013-01-29T17:27:53.853 に答える