【发布时间】:2012-11-25 11:17:44
【问题描述】:
假设我有一个 LinearLayout(width & height match_parent) 与 weightSum = "5",然后以 水平方式
我将 5 TableLayout(width & height wrap_content) 与 layout_weight = "1" 放在一起,因此每个 TableLayout 都是其父级的 1/5。
但是,在那之后,在每个 TableLayout 我放了一个 TextView(width & height wrap_content) 并且每件事都出错了(从我的角度来看):
如果文本太长,则它强制其父布局(在我们的例子中为 TableLayout)expend,因此 TableLayout 的 1/5 比例来自其 LinearLayout即使我指定了 TextView,s ellipsize="end",也不再尊重父级。
在做了一些研究之后,我发现设置 layout_width 比其他所有 属性(如 weightSum) 都强。
在我的例子中,我设置了 TableLayout 的 width=wrap_content,但也设置了 layout_weight="1",但因为它是 wrap_content,它倾向于在其内容之后扩展而不是尊重重量。
谁能给我一些解决方案,我的表格布局如何尊重重量?(因为我不希望我的布局不尊重比例并在子文本视图的长度之后消耗)。
谢谢你的好意。这是我的代码:
<!-- my code -->
< LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="5" >
<!-- table 1 -->
<TableLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:ellipsize="end"
android:maxLines="1"
android:text="test1test2test3test4test5"
android:textSize="20dp"/>
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</TableLayout>
</TableLayout>
<!-- table 2 -->
<TableLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="test"
android:textSize="20dp" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</TableLayout>
</TableLayout>
<!-- table 3 -->
<TableLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="test"
android:textSize="20dp" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</TableLayout>
</TableLayout>
<!-- table 4 -->
<TableLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="test"
android:textSize="20dp" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</TableLayout>
</TableLayout>
<!-- table 5 -->
<TableLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="test"
android:textSize="20dp" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</TableLayout>
</TableLayout>
</LinearLayout>
【问题讨论】:
标签: android android-layout layout android-linearlayout