android中的布局问题(两个按钮之间的省略文本)
试图使工具栏两边都有两个按钮,中间有标题。当文本太长时,标题应为省略号,如下所示:android中的布局问题(两个按钮之间的省略文本),android,android-layout,Android,Android Layout,试图使工具栏两边都有两个按钮,中间有标题。当文本太长时,标题应为省略号,如下所示: [Button] Some quite long header te... [Button] <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#444" android
[Button] Some quite long header te... [Button]
<TableLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#444"
android:stretchColumns="1">
<TableRow android:background="#777" android:minHeight="60dp">
<Button android:text="Left"/>
<TextView android:text="Center this very long text and ellisize"
android:background="#f00"
android:lines="1"
android:ellipsize="end"
android:scrollHorizontally="true"
android:gravity="center"
/>
<Button android:text="Right"/>
</TableRow>
</TableLayout>
我试过几种解决办法,但都没有用。我最后一次尝试是这样的:
[Button] Some quite long header te... [Button]
<TableLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#444"
android:stretchColumns="1">
<TableRow android:background="#777" android:minHeight="60dp">
<Button android:text="Left"/>
<TextView android:text="Center this very long text and ellisize"
android:background="#f00"
android:lines="1"
android:ellipsize="end"
android:scrollHorizontally="true"
android:gravity="center"
/>
<Button android:text="Right"/>
</TableRow>
</TableLayout>
但右边的按钮仍然离开屏幕
更新:
解决办法是:
<RelativeLayout android:layout_width="fill_parent" android:layout_height="64dip">
<Button android:id="@+id/btnLeft"
android:text="Left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button android:id="@+id/btnRight"
android:text="Right"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:text="Center this very long text and ellisize"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:background="#f00"
android:lines="1"
android:ellipsize="end"
android:gravity="center"
android:scrollHorizontally="true"
android:layout_toLeftOf="@id/btnRight"
android:layout_toRightOf="@id/btnLeft"
/>
</RelativeLayout>
更新2:以及使用TableLayout的第二个解决方案:
<TableLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#444"
android:stretchColumns="1"
android:shrinkColumns="1">
<TableRow android:background="#777" android:minHeight="60dp">
<Button android:text="Left"/>
<TextView android:text="Center this very long text and ellisize"
android:background="#f00"
android:lines="1"
android:ellipsize="end"
android:scrollHorizontally="true"
android:gravity="center"
/>
<Button android:text="Right"/>
</TableRow>
</TableLayout>
尝试使用相对布局而不是表布局。如果将两个按钮分别设置为左对齐和右对齐,则可以使用layout_alignRight和layout_alignLeft属性将文本字段设置为两个按钮之间的范围。您可以查看android开发网站上给出的RelativeLayout示例代码。虽然RelativeLayout应该可以工作,但另一个选项是将LinearLayout设置添加到Horizontal。将这三个小部件设置为布局宽度:填充父项,并为TextView和两个按钮分别赋予权重1,如果您希望它们平均占据屏幕的三个部分
现在不需要在计算机上测试它,但理论上应该是可行的。我使用了一些不同的属性,但你给我指明了正确的方向。最后的工作代码是:`android:layout\u-toLeftOf=@id/btnRight android:layout\u-toRightOf=@id/btnLeft`您的表很好,但您需要在XML中将列索引1设置为可收缩。现在,您允许它根据需要进行拉伸。太好了,谢谢,这种方法也非常有效!用这两种解决方案更新了问题