在LinearLayout中设置TextView的宽度

8

我正在使用一个Listview的头部。Listview头部有三列,分别是a、b、c。我使用两个LinearLayout来设计Listview头部,如下所示:

<?xml version="1.0" encoding="utf-8"?>   
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
 android:layout_width="fill_parent"  
 android:layout_height="40dip"  
 android:padding="0dip">  
 <LinearLayout  
    android:orientation="horizontal"  
    android:background="#1e90ff"  
    android:layout_width="0dip"  
    android:layout_weight="1"  
    android:padding="5dip"  
    android:layout_height="40dip">  
    <TextView  
        android:id="@+id/a"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
        android:textColor="#000000"  
        android:layout_weight="1"  
        android:gravity="center_vertical"  
        android:text="@string/a"  
    />  
    <TextView  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
        android:textColor="#000000"  
        android:layout_weight="1"  
        android:gravity="center_vertical"  
        android:id="@+id/b"  
        android:singleLine="true"  
        android:text="@string/b"  
    />  
    <TextView  
        android:layout_width="fill_parent"   
        android:layout_height="fill_parent"  
        android:textColor="#000000"  
        android:layout_weight="1"  
        android:gravity="center_vertical"  
        android:id="@+id/c"  
        android:singleLine="true"  
        android:text="@string/c"  
    />  
 </LinearLayout>   
</LinearLayout>

现在我想分别固定列a、b、c的宽度。如何设置这些列的宽度?另外,使用LinearLayout是否是一个好的做法?请给予建议。

4个回答

15

要将您的文本视图的 layout_width 更改为固定宽度,请使用:

 android:layout_width="40dip"

如果您希望它们占据屏幕的百分比,请更改layout_weight

 android:layout_weight="2"

在Android中,所有元素的权重(例如您在此处使用的线性布局)将被相加,然后使用每个视图的权重来定义它占用多少空间。例如,如果您将a,b,c的权重分别设置为2、3、5,则a将占20%的宽度,b将占30%的宽度,c将占50%的宽度。
如果您想要列,请考虑使用表格布局(教程

1
非常感谢这个简单明了的解释。我对布局还不熟悉,这节省了我很多时间。 - BrunoPugliese

4
你也可以按照以下方式设置百分比宽度。
<LinearLayout 
    android:orientation="horizontal" 
    android:background="#1e90ff" 
    android:layout_width="0dip" 
    android:layout_weight="1" 
    android:padding="5dip" 
    android:layout_height="40dip">

    <!--
      android:layout_weight="0.5"  <= it will set the 50 % width of screen
    -->
    <TextView 
        android:id="@+id/a" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:textColor="#000000"  
        android:layout_weight="0.5"  
        android:gravity="center_vertical"  
        android:text="@string/a"  
    />  
    <TextView  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
        android:textColor="#000000"  
        android:layout_weight="0.5"  
        android:gravity="center_vertical"  
        android:id="@+id/b"  
        android:singleLine="true"  
        android:text="@string/b"  
    />  
</LinearLayout>

需要注意的一点是,设置layout_weight属性的具体数字并不重要,比例才是关键。例如,如果将0.5替换为1.0或5.0,只要在所有地方都进行了替换,您的XML就会完全执行相同的操作。尽管您的小数可能很有帮助,因为您可以轻松计算并查看它们是否加起来为1.0。 - Andreas Eriksson

2
您可以使用 TextViewsetWidth() 方法动态设置大小。

0

如果您正在使用ListView中使用的布局中进行此操作,则仅在代码中动态设置宽度有效。实际上,我的列表中有三个TextView,但为了简洁起见,我只显示了其中两个。以下是来自布局的XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:weightSum="100">

    <TextView
        android:id="@+id/sl_name"
        android:layout_width="wrap_content"
        android:layout_weight="40"
        android:layout_height="wrap_content"
        android:textSize="16sp"/>

    <TextView
        android:id="@+id/sl_score"
        android:textSize="16sp"
        android:gravity="right"
        android:paddingRight="8dp"
        android:layout_width="wrap_content"
        android:layout_weight="15"
        android:layout_height="wrap_content"/>
</LinearLayout>

这是ListView适配器中的代码:

public View getView(int position, View cvtView, ViewGroup parent)
  {
    int width = parent.getWidth();
    View rowView = inflater.inflate(R.layout.scorelayout, parent, false);
    String str = values.get(position);
    String[] vals = str.split("\t");

    TextView tvName = (TextView)rowView.findViewById(R.id.sl_name);
    tvName.setWidth((int)(width *.30));
    tvName.setText(vals[2]);

    TextView tvScore = (TextView)rowView.findViewById(R.id.sl_score);
    int stemp = Integer.parseInt(vals[0]);
    tvScore.setWidth((int)(width * .15));
    tvScore.setText(Integer.toString(stemp));
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接