在 LinearLayout 中设置 textview 的宽度

2023-11-23

我正在使用列表视图的标题。 ListView 标题有三列。说a、b、c。我使用两个 LinearLayouts 来设计 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 是一个好习惯吗?请指教。


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

 android:layout_width="40dip"

或者您希望它们占据屏幕的百分比更改layout_weight

 android:layout_weight="2"

在 android 中,另一个视图中的所有元素的权重(例如这里的线性布局)将被添加在一起,然后使用每个视图的权重来定义它需要多少空间。 IE。如果 a、b、c 的权重分别为 2、3、5,则 a 的宽度为 20%,b 的宽度为 30%,c 的宽度为 50%。

如果您想要列,您应该考虑使用表格布局(tutorial)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 LinearLayout 中设置 textview 的宽度 的相关文章

随机推荐