Android 在 LinearLayout 中设置 textview 的宽度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2676190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:52:16  来源:igfitidea点击:

Setting width of textview in a LinearLayout

android

提问by cppdev

I am using a header for a Listview. ListView header has three columns. Say a,b,c. I am using two LinearLayouts to design ListView header as shown below:

我正在为 Listview 使用标头。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>

Now i want to fix the width of columns a, b, c respectively. How to set width of these columns ? Again, is it a good practise to use LinearLayout for this ? Please Advise.

现在我想分别修复 a、b、c 列的宽度。如何设置这些列的宽度?同样,为此使用 LinearLayout 是一个好习惯吗?请指教。

回答by stealthcopter

To change the layout_width of your textviews to a fixed width use:

要将 textviews 的 layout_width 更改为固定宽度,请使用:

 android:layout_width="40dip"

Or of you want them to take up percentages of the screen change the layout_weight

或者你想让他们占据屏幕的百分比改变 layout_weight

 android:layout_weight="2"

In android the weight of all elements within another one (such as your linear layout here) will be added together and then using the weight of each view to define how much space it takes. I.E. if you had the weights as 2,3,5 for a,b,c respectively, then a would be 20% wide, b 30% wide and c 50% wide.

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

If you want columns, you should consider using a table layout (tutorial)

如果你想要列,你应该考虑使用表格布局(教程

回答by mukesh yadav

you can also set the width in percentage as follow

您还可以按百分比设置宽度如下

<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>

回答by Ashish

You can use the setWidth()method of TextViewto set the size dynamically

您可以使用 的setWidth()方法TextView动态设置大小

回答by Prof. John

If you're doing this within a layout that is used in a ListView, only setting the width dynamically in code works. I actually have three TextViews in my list, but I'm only showing two here, for brevity. Here is XML from the layout:

如果您在 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>

Here is code from within the Adapter for the ListView:

下面是 ListView Adapter 中的代码:

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));
}