簡體   English   中英

無法在RelativeLayout中對齊ImageView

[英]Can't Align ImageView in RelativeLayout

我有一個包含兩個textViews和一個ImageView的布局。

第一個文本左對齊。 第二個也左對齊,並在第一個文本下方。 圖像與右側對齊! 並與第一個文本的頂部和第二個文本的底部對齊。

我嘗試使用RelativeLayout進行作業,但Image拒絕向右對齊。

這是我的xml

<RelativeLayout
            android:id="@+id/NameAndPhoto"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
            <LinearLayout
                android:id="@+id/Names"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/FirstName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="@style/contact_page_name_text_style" >
                </TextView>

                <TextView
                    android:id="@+id/LastName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="@style/contact_page_name_text_style" >
                </TextView>
            </LinearLayout>

            <ImageView
                android:id="@+id/PersonPhoto"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@id/Names"
                android:layout_alignParentRight="true"
                android:layout_alignTop="@id/Names"
                android:layout_toRightOf="@id/Names"
                android:contentDescription="@string/contact_page_photo_content_description"
                android:padding="3sp"
                android:scaleType="fitCenter" >
            </ImageView>
        </RelativeLayout>

請指教。

謝謝,伊卡

如果要使用LinearLayout,請使用width="match_parent"並通過設置gravity="right"對齊。 因此,您可以在具有父級寬度的視圖中移動文本

至於將ImageView放在RelativeLayout中,則存在一個矛盾: android:layout_alignParentRight="true"android:layout_toRightOf="@id/Names" -兩個不同的水平放置。

另外,您不能同時使用底部和頂部放置。 您正在Image中使用它們。 再次成為矛盾。

又如何將Image放置在LinearLayout的右側,該圖像占據了根布局的所有寬度? 哪里可以把它放在哪里? 將名稱的寬度更改為wrap_content

我不確定您要尋找的是什么...也不知道嵌套大量LienarLayouts是否是個好習慣...但這可能會有所幫助嗎?

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/Names"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:orientation="vertical" android:layout_weight="1">

        <TextView
            android:id="@+id/FirstName"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="first name" />

        <TextView
            android:id="@+id/LastName"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="last name" />
    </LinearLayout>

    <ImageView
        android:id="@+id/PersonPhoto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="3sp"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_launcher" android:layout_weight="1"/>

</LinearLayout>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM