Reputation: 1723
I have two TextView in a RelativeLayout. How to set the TextViews if I want those always (different screen resolution for example 540x960) the width of each TextView is the 50 percent of the full UI width? I would set size of both TextView to always 50 percent of full width of UI. Need to stretching of TextViews on different screen resolutions. How can I do this?
Upvotes: 10
Views: 13161
Reputation: 5034
Whenever split some view like (Button , Text View) in equally in Horizontal / Vertical
android:weight Sum / android:layout_weight Technique use...
Must remember one thing these technique must support in Linear Layout only but here you want to use Relative Layout so that simply put your Linear Layout inside Relative Layout ..
Must Follow These Rules :
1> set android:weight Sum into your Linear Layout
2> set android:layout_width each child View inside "0dp"
3> set android:layout_weight each child View depend upon how many child view & android:weight Sum(e.g: android:weight Sum = "2" & two child view then layout_weight="1" , layout_weight="1")
Ex:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="child View 1" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="child View 2"/>
</LinearLayout>
</RelativeLayout>
if Layout Orientation is Vertical then only set android:layout_height is "0dp" instead of android:layout_width
Upvotes: 13
Reputation: 7569
Set the layout_width
to fill_parent
for both TextView
s and give them the same layout_weight
like 1.0
and that should do it (or not give either one a layout_weight
).
Something like this should do the trick:
<LinearLayout
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
orientation = "horizontal">
<TextView
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:text = "Text 1"
/>
<TextView
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:text = "Text 2"
/>
</LinearLayout>
Upvotes: 3
Reputation: 11112
You can try using weight with creating an linear layout in your relative layout then set the android:layout_width="fill_parent"
and the android:weightSum = 2
and the android:orientation="horizontal"
, after that place the textview inside the linear layout and set the weight of each textview 1.
<RelativeLayout
android:layout_width = "fill_parent"
android:layout_height = "wrap_content">
<LinearLayout
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:orientation="horizontal"
android:weightSum = "2">
<TextView
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:text = "YOUR FIRST TEXT"
android:weight = "1"
/>
<TextView
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:text = "YOUR SECOND TEXT"
android:weight = "1"
/>
</LinearLayout>
</RelativeLayout>
the weight sum will be used to measured the portion of the textview like if you give the weight sum 3 and give 1 textview weight = 2 and another textview weight = 1 the layout will be 2/3 (66.666%) and 1/3 (33.3333%) so if you set it 2 and give every textview weight = 1 it should be 50% and 50%
It works for me, if you have any question feel free to ask in the comment :)
Upvotes: 8