在一行中具有不同列宽的Android TableLayout
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23786685/
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
Android TableLayout With Different Column's Width In A Row
提问by paolo2988
I am trying to do my first complex GUI, but now i can't to solve this problem.
我正在尝试做我的第一个复杂的 GUI,但现在我无法解决这个问题。
The first column of the first two rows only needs to contain the label, while the second column of the first two rows must occupy the remaining space.
前两行的第一列只需要包含标签,而前两行的第二列必须占据剩余空间。
In this snapshot the problem that i noticed is that the first column is larger than i would like.
在这个快照中,我注意到的问题是第一列比我想要的要大。
This is the piece of code that implements that piece of layout.
这是实现该布局的一段代码。
...
...
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/linear_layout_background"
android:stretchColumns="*"
android:shrinkColumns="*">
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp" >
<TextView
android:id="@+id/textViewPlateValueLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/plateValue"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/productID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/back"
android:ems="5"
android:text="@string/blank"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp" >
<TextView
android:id="@+id/ztlLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ztl"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/ztlShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/back"
android:text="@string/blank"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:gravity="center" >
<TextView
android:id="@+id/isAllowed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/blank"
android:background="@drawable/back"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
</TableLayout>
...
...
In addition, since they are really impractical, I would like to ask you some advice on the best approaches to use than this.
此外,由于它们确实不切实际,因此我想请教您一些关于除此之外的最佳使用方法的建议。
回答by CanDroid
You can try using layout_weight like this:
您可以尝试使用 layout_weight 像这样:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="horizontal"
tools:context=".MainActivity" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#ff00ff"
android:stretchColumns="*"
android:shrinkColumns="*">
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp" >
<TextView
android:id="@+id/textViewPlateValueLabel"
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Label1:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/productID"
android:layout_weight="7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#00ff00"
android:ems="5"
android:text="Some Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp" >
<TextView
android:layout_weight="3"
android:id="@+id/ztlLabel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Label2:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_weight="7"
android:id="@+id/ztlShow"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#00ff00"
android:text="Some Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:gravity="center" >
<TextView
android:id="@+id/isAllowed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some Text"
android:background="#0088ff"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
</TableLayout>
</LinearLayout>
回答by i41
I wouldn't use TableLayout, if I were you. I'd probably use LinearLayout or RelativeLayout. You can get the same effect with some of the XML attributes. This is akin to using HTML's table
attribute vs. using div
s to simulate tables.
如果我是你,我不会使用 TableLayout。我可能会使用 LinearLayout 或 RelativeLayout。您可以使用某些 XML 属性获得相同的效果。这类似于使用 HTML 的table
属性与使用div
s 来模拟表格。
So, instead of a table, you can have LinearLayout inside of LinearLayout to simulate a table.
因此,您可以在 LinearLayout 内部使用 LinearLayout 来模拟表格,而不是表格。
回答by Idan
Although canDroid, could be sufficient to some of us, it didn't worked for me, I tried all kind of tricks to make the TableLayout
to work. I found LinearLayout
much easier to use, here is an example of TableLayout
"like" layout using LinearLayout
, few cells with a thumbnail, and few without.
尽管canDroid对我们中的一些人来说可能已经足够了,但它对我不起作用,我尝试了各种技巧来使其TableLayout
工作。我发现LinearLayout
更容易使用,这里有一个TableLayout
“喜欢”布局的例子,使用LinearLayout
,很少有带有缩略图的单元格,很少有没有缩略图。
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp">
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="94dp"
android:layout_margin="5dp"
android:text="@string/nut1" />
</LinearLayout>
<View
android:layout_width="395dp"
android:layout_height="2dp"
android:background="@color/colorPrimary"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="0dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp">
<ImageView
android:layout_width="94dp"
android:layout_height="94dp"
android:adjustViewBounds="false"
android:background="@drawable/one"
android:contentDescription="@string/nut3"
android:cropToPadding="false"
android:saveEnabled="false"
android:scaleType="center" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="94dp"
android:layout_margin="10dp"
android:text="@string/nut3" />
</LinearLayout>
</LinearLayout>