How to align an element to the right of its parent center? (Android layouts)

I want something like this:
enter image description here

i using android relativelayout and this is a tag like: android: layout_centerHorizontal = "true" and this gives me the following:

enter image description here

i player with other tags and combine them, but will fail.

is there any way to do this?

+4
source share
4 answers

Try the following:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/linearLayout1" android:layout_width="20dp" android:layout_height="match_parent" android:layout_centerHorizontal="true" android:background="@android:color/black" > </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/linearLayout1" android:text="YOUR TEXT" /> </RelativeLayout> 
+10
source

try it

 <RelativeLayout android:layout_width="fill_parent" android:background="#FFFFFF" android:layout_height="wrap_content"> <View android:layout_width="10dp" android:layout_height="1dp" android:layout_centerInParent="true" android:id="@+id/dummy" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="SDADASd" android:layout_centerVertical="true" android:layout_toRightOf="@id/dummy" android:textColor="#000000" /> </RelativeLayout> 
+3
source
  or go with linearlayout... <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/linearLayout1" android:layout_width="0dp" android:layout_height="match_parent" android:gravity="right" android:layout_weight="1" > <View android:layout_width="10dp" android:background="@android:color/black" android:layout_height="match_parent"/> </LinearLayout> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:layout_toRightOf="@+id/linearLayout1" android:text="YOUR TEXT" /> </LinearLayout> 
+2
source

Add a transparent view to the center of your RelativeLayout and

 height="match_parent" width="15dp"//(it doesn't matter much) 

then specify the layout parameter relative to this view, for example

 android:layout_alignRight="@+id/yourView" 
+1
source

Source: https://habr.com/ru/post/1501610/


All Articles