开发者

Setting width of textview in a LinearLayout

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:

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


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

 android:layout_width="40dip"

Or of you want them to take up percentages of the screen change the 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.

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


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>


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


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:

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜