开发者

Calculate correct values to use in layout_weight?

I have a design that I want to position the views into these "section" correctly:

Calculate correct values to use in layout_weight?

The red areas are empty.

The other "areas" should be centered, the green is ImageView, the other two are TextViews.

To make this possible, someone from this site told me I should use layout_weight.. how can I calculate the correct percentage to use?

What I did was: Padding on the red sides, and calculated the rest according to the left space.. but the views werent correct.. the xml i tried:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:orientation="horizontal"
android:layout_height="wrap_content"  a开发者_JAVA技巧ndroid:paddingTop="10dip"    android:paddingBottom="10dip">

<TextView android:id="@+id/Start_Numbering" android:textSize="19.5dip"
    android:layout_width="0dp" android:layout_height="wrap_content"
    android:layout_weight="0.4" 
    android:gravity="center"
    />

<ImageView android:id="@+id/Start_ImageView"
    android:layout_weight="0.11" android:layout_height="wrap_content"
    android:layout_width="0dp" android:src="@drawable/quran_list_noaudioavailable"
    android:gravity="center"
    ></ImageView>


<TextView android:id="@+id/Start_Name" android:textColor="#a7e9fe"
    android:textSize="21dip" android:layout_width="0dp"
    android:layout_weight="0.6"
    android:gravity="center"
    android:layout_height="wrap_content" />

 </LinearLayout>


layout_weight uses a fraction to determine the size of the children:

child_space = child_weight / total_weight

where total_weight is the weight sum of all children in the parent layout.

E.g. if you want to have 3 views, two filling 25% of the width and one filling 50%, assign 1 to the first two and 2 to the second. This results in 1/4 (=25%) of the space for each of the two and 2/4 (= 50%) for the third one.

(you could also use any other number for the first two and double the first weight for the 3rd view, this cancels down to the above fractions)

You can also just give numbers below 1 as you did above. As long as the total sum is 1 these are interpreted as percentages (e.g. 0.42 = 42%).


Try assigning 1 to the value of the padding on the left and make the others multiples of that. (i.e. 1,3,1,5,1)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜