2012-09-07 32 views
3

我有这样的布局,硬编码的宽度:Android - 3列行布局 - 如何让它占用屏幕的百分比?

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:paddingTop="4dip" 
    android:paddingBottom="6dip" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textSize="13sp" 
    android:weightSum="1.0"> 

    <TextView android:id="@+id/TRAIN_CELL" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight=".60"/> 




     <TextView android:id="@+id/FROM_CELL" 
     android:layout_width="0dp" 
     android:textSize="20sp" 
     android:textStyle="bold" 
     android:gravity="center" 
     android:textColor="@color/light_best_blue" 
     android:layout_height="wrap_content" 
     android:layout_weight=".20"/> 


    <TextView android:id="@+id/TO_CELL" 
     android:layout_width="0dp" 
     android:textSize="20sp" 
     android:textStyle="bold" 
     android:gravity="center" 
     android:layout_weight=".20" 
     android:textColor="@color/light_best_blue" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 

但是,有没有办法给每列的宽度的百分比?

谢谢!

回答

3

使用

layout_width="0dp" 
layout_weight="x" 

其中x是你的每个元素的百分比。请参阅http://developer.android.com/guide/topics/ui/layout/linear.html

+0

我想这也不太工作的一些不同的东西。我在最初的问题中发布了我的最新版式。你能注意到那里可能不是那个地方吗?谢谢! – Genadinik

+0

没有什么似乎。也许使用0.20而不是.20,或甚至使用整数(60,20,20) – njzk2

2

您将weight属性和宽度设置为0dp,并在父级中指定weightSum。

在你的情况下,设置线性布局的android:weightSum为1,并将子体重除以0.3,0.3和0.4。 (机器人:layout_weight = “0.3”)。

编辑:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:paddingTop="4dip" 
android:paddingBottom="6dip" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:textSize="13sp" 
android:weightSum="1.0" 
android:orientation="horizontal"> 

<TextView android:id="@+id/TRAIN_CELL" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight=".60"/> 




    <TextView android:id="@+id/FROM_CELL" 
    android:layout_width="0dp" 
    android:textSize="20sp" 
    android:textStyle="bold" 
    android:gravity="center" 
    android:layout_height="wrap_content" 
    android:layout_weight=".20"/> 


<TextView android:id="@+id/TO_CELL" 
    android:layout_width="0dp" 
    android:textSize="20sp" 
    android:textStyle="bold" 
    android:gravity="center" 
    android:layout_weight=".20" 
    android:layout_height="wrap_content" /> 

</LinearLayout> 
+0

现在尝试 - 谢谢:) – Genadinik

+0

,这并没有很好的工作。我用新的布局更新了我的问题,现在左侧占用了屏幕的90%,其余两列没有任何内容。 – Genadinik

+0

将父线性布局的方向设置为水平。 – Gan