簡體   English   中英

將相對布局中的中心視圖與相對布局外的另一個視圖對齊

[英]Align center view in relativealyout to another view outside of the relativelayout

我需要使用 TextView“標題”使 RelativeLayout 及其兩個視圖居中。 當RelativeLayout 中的兩個視圖都存在時,這會起作用。 但是,有時(基於某些條件)我不需要顯示“箭頭”ImageView。 在這種情況下,RelativeLayout 中的 textView“phone_name”與 TextView“title”不居中。 有點偏左。 我怎樣才能使這兩種情況都有效?

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingBottom="16dp">
        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="32sp"
            android:layout_gravity="center"
            android:ellipsize="end"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:paddingBottom="8dp"
            android:textColor="@color/mineral"
            android:scrollHorizontally="true"
            android:maxLines="1"
            app:customTypeface="@string/font_sharp_sans_bold"
            tools:text="Hello Joel"/>
        <RelativeLayout
            android:id="@+id/select_phone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="12dp"
            android:background="@drawable/bg_selector">
            <TextView
                android:id="@+id/phone_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="14sp"
                android:layout_centerInParent="true"
                android:ellipsize="end"
                android:textColor="@color/charcoal"
                android:maxLines="1"
                app:customTypeface="@string/font_sharp_sans_semi_bold"
                tools:text="Samsung Galaxy"/>
            <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/arrow"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_drop_down"
                android:layout_centerVertical="true"
                android:rotation="180"
                android:layout_alignParentRight="true"/>
        </RelativeLayout>
    </LinearLayout>
</layout>

您可以嘗試以下解決方案 - 當 ImageView 可見性消失時,它會使 phone_name 居中 -

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingBottom="16dp">
        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="32sp"
            android:layout_gravity="center"
            android:ellipsize="end"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:paddingBottom="8dp"
            android:textColor="@color/mineral"
            android:scrollHorizontally="true"
            android:maxLines="1"
            app:customTypeface="@string/font_sharp_sans_bold"
            tools:text="Hello Joel"/>
        <RelativeLayout
            android:id="@+id/select_phone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="12dp"
            android:gravity="center"
            android:background="@drawable/bg_selector">
            <TextView
                android:id="@+id/phone_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="14sp"
                android:layout_centerInParent="true"
                android:ellipsize="end"
                android:textColor="@color/charcoal"
                android:maxLines="1"
                app:customTypeface="@string/font_sharp_sans_semi_bold"
                tools:text="Samsung Galaxy"/>
            <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/arrow"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_drop_down"
                android:layout_centerVertical="true"
                android:rotation="180"
                android:layout_alignParentRight="true"/>
        </RelativeLayout>
    </LinearLayout>
</layout>

暫無
暫無

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

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