简体   繁体   中英

How to get two fields, side by side in relative layout (android)?

I know that you can use weight parameters for linear layout in order to make two fields align nicely. What I want to do is I want to make sure that left half of the screen is used by one text field and other half is used by other text field (I am talking about width). How to do so?

Use a "hidden view", with no height or width, in the center and put the text views on either side. Use parent align to set left and right.

<RelativeLayout 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content">

   <View android:id="@+id/dummy"
       android:layout_height="0dp" 
       android:layout_width="0dp"
       android:layout_centerInParent="true"/>

   <TextView
       android:layout_alignRight="@id/dummy"
       android:layout_height="wrap_content"
       android:layout_width="wrap_content"
       android:layout_alignParentLeft="true"/>

   <TextView
       android:layout_alignLeft="@id/dummy"
       android:layout_height="wrap_content"
       android:layout_width="wrap_content"
       android:layout_alignParentRight="true"/>

</RelativeLayout>

Can you calculate width dynamically? Maybe you could use 2 RelativeLayouts in a horizontal LinearLayout?

This is not possible with RelativeLayour as a parent. You need to wrap these TextViews inside a LinearLayout and set the widths with layout weight.

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