简体   繁体   中英

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". This works when both the views in the RelativeLayout are present. However, sometimes (based on some condition) i don't need to show the "arrow" ImageView. In this case the textView "phone_name" inside the RelativeLayout is not center with the 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 -

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM