Reputation: 18649
I currently have a 2 column layout which looks like this:
<TextView android:id="@+id/TRAIN_CELL"
android:layout_width="275dip"
android:layout_height="wrap_content"
android:textSize="16sp"/>
<TextView android:id="@+id/TO_CELL"
android:layout_width="25dip"
android:textSize="20sp"
android:textStyle="bold"
android:gravity="center"
android:textColor="@color/light_best_blue"
android:layout_height="wrap_content"
android:layout_weight="1"/>
But with this approach I just hardcode the widths, and I am not sure what happens on wider on narrower screens. Is there a way to adjust this to use percentages of the width?
Thanks!
Upvotes: 1
Views: 144
Reputation: 6867
You can't specify width, or height using percentages, but there's something that is similar - weightSum
and layout_weight
. If you for example what your View
to have width half of a screen, you have to do it like this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:weightSum="1.0">
<View
android:id="@+id/view"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:layout_width="0dip" />
</LinearLayout>
As you can see, the container of a View
(LinearLayout
) has set android:weightSum
to 1.0
which means that 1.0
will be 100% of it's width/height. Then if you specify android:layout_weight
for View
inside this container to for example .5
and set it's width or height to 0dp
(this part is very important as if you miss it, the View
will use it's specified value instead of calculated one according to weights) it will receive 50% value of it's container's width/height.
Upvotes: 3
Reputation: 39578
android:layout_weight="1/2/3/4/5/..."
will be your new friend ;-)
Do not forget to also use android:layout_width="0dip"
Upvotes: 1