In my android app, I have a relative layout whoes width fills the parent. How can I create 3 views so that the first view is a textview and is exactly 100dp in width, the third view is a edittext that is also 100dp wide, and the 2nd is a textview whoes width that fills up the center space.
[][ ][]
view 1, view 2, view 3
However, the first has to be left aligned, and the third has to be right aligned. The 2nd, needs to be in between, but fills up the space.
Note: In java code.
You can use layout_toLeftOf
and layout_toRightOf
attributes to set the anchor points, and then set the layout_width="wrap_content"
to stretch the view.
Here is a sample layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/tv1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Text 1" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/et1"
android:layout_toRightOf="@+id/tv1"
android:text="Text 2" />
<EditText
android:id="@+id/et1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:hint="Text 3" />
</RelativeLayout>
Try it out:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textview1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="TextView1" />
<TextView
android:id="@+id/textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/edittext1"
android:layout_toRightOf="@+id/textview1"
android:text="TextView2" >
</TextView>
<EditText
android:id="@+id/edittext1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:hint="EditText3" />
</RelativeLayout>
I think you could try LinearLayout in this case. Set the 1st and 3rd with weight 0, 2nd with weight 1 and fill_parent.
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.