简体   繁体   English

将相对布局中的中心视图与相对布局外的另一个视图对齐

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

I need to make the RelativeLayout and its two Views center with the TextView "title".我需要使用 TextView“标题”使 RelativeLayout 及其两个视图居中。 This works when both the views in the RelativeLayout are present.当RelativeLayout 中的两个视图都存在时,这会起作用。 However, sometimes (based on some condition) i don't need to show the "arrow" ImageView.但是,有时(基于某些条件)我不需要显示“箭头”ImageView。 In this case the textView "phone_name" inside the RelativeLayout is not center with the TextView "title".在这种情况下,RelativeLayout 中的 textView“phone_name”与 TextView“title”不居中。 It's a bit off to the left.有点偏左。 How can I make this both cases work?我怎样才能使这两种情况都有效?

<?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>

you can try below solution - it makes phone_name center when ImageView visibility gone -您可以尝试以下解决方案 - 当 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