繁体   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