比较来自世界各地的卖家的域名和 IT 服务价格

设置宽度 textview 在 LinearLayout

我使用标题 Listview. ListView 标题有三列。 告诉我 a,b, c. 我用二 LinearLayouts 用于开发标题 ListView, 如下所示:


xml version="1.0" encoding="utf-8"?
<linearlayout android:layout_height="40dip" android:layout_width="fill_parent" android:padding="0dip" xmlns:android="[url=http://schemas.android.com/apk/res/android">]http://schemas.android.com/apk ... gt%3B[/url]
<linearlayout android:background="#1e90ff" android:layout_height="40dip" android:layout_weight="1" android:layout_width="0dip" android:orientation="horizontal" android:padding="5dip">
<textview android:gravity="center_vertical" android:id="@+id/a" android:layout_height="fill_parent" android:layout_weight="1" android:layout_width="fill_parent" android:text="@string/a" android:textcolor="#000000"></textview>
<textview android:gravity="center_vertical" android:id="@+id/b" android:layout_height="fill_parent" android:layout_weight="1" android:layout_width="fill_parent" android:singleline="true" android:text="@string/b" android:textcolor="#000000"></textview>
<textview android:gravity="center_vertical" android:id="@+id/c" android:layout_height="fill_parent" android:layout_weight="1" android:layout_width="fill_parent" android:singleline="true" android:text="@string/c" android:textcolor="#000000"></textview>
</linearlayout>
</linearlayout>


现在我想修复列宽度 a, b, c 分别。 如何设置这些列的宽度 ? 再一次,是一个很好的做法 LinearLayout ? 请指教。
已邀请:

奔跑吧少年

赞同来自:

为了改变 layout_width 您的文本介绍固定宽度,使用:


android:layout_width="40dip"


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


android:layout_weight="2"


在 android 另一个内部的所有元素的重量 /例如,这里的线性布局/ 它将被折叠在一起,然后使用每个物种的重量来确定所需的空间。 I.E. 如果您分别具有2,3,5,b,c,则重量,那么 20% 宽阔,宽 30% 和 c 50% 宽的。

如果需要列,则应考虑使用表布局 /
http://developer.android.com/g ... .html
/

詹大官人

赞同来自:

您还可以将宽度设置为百分比,如下所示。


<linearlayout android:background="#1e90ff" android:layout_height="40dip" android:layout_weight="1" android:layout_width="0dip" android:orientation="horizontal" android:padding="5dip">
<!--
android:layout_weight="0.5" <= it will set the 50 % width of screen
-->
<textview android:gravity="center_vertical" android:id="@+id/a" android:layout_height="fill_parent" android:layout_weight="0.5" android:layout_width="fill_parent" android:text="@string/a" android:textcolor="#000000"></textview>
<textview android:gravity="center_vertical" android:id="@+id/b" android:layout_height="fill_parent" android:layout_weight="0.5" android:layout_width="fill_parent" android:singleline="true" android:text="@string/b" android:textcolor="#000000"></textview>
</linearlayout>

莫问

赞同来自:

您可以使用该方法
setWidth//


TextView

用于动态尺寸安装

小明明

赞同来自:

如果您在使用的布局中执行它 ListView, 这只是代码工作中宽度的动态设置。 事实上,我有三个 TextViews 在我的名单中,但我只向她展示两个简洁。 这里 XML 从布局:


xml version="1.0" encoding="utf-8"?
<linearlayout android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="horizontal" android:weightsum="100" xmlns:android="[url=http://schemas.android.com/apk/res/android">]http://schemas.android.com/apk ... gt%3B[/url]
<textview android:id="@+id/sl_name" android:layout_height="wrap_content" android:layout_weight="40" android:layout_width="wrap_content" android:textsize="16sp"></textview>
<textview android:gravity="right" android:id="@+id/sl_score" android:layout_height="wrap_content" android:layout_weight="15" android:layout_width="wrap_content" android:paddingright="8dp" android:textsize="16sp"></textview>
</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//;
}

要回复问题请先登录注册