带有椭圆和按钮的 TextView
我正在尝试创建一个带有左右按钮的标题,标题位于中心,按钮右侧有一个按钮(这将允许选择某些内容)。布局应该是这样的:
|<Button> <TextView><Button> <Button>|
如果标题很长,则应该省略。为此,文本视图似乎需要一个layout_weight,如此解决方案,否则会将右侧按钮推离屏幕。然而,设置文本视图的layout_weight会将中心按钮推到右侧:
|<Button> <TextView> <Button><Button>|
现在的布局是这样的:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:text="Hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:text="Testing"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:singleLine="true"
android:layout_weight="1"
android:gravity="center"
android:ellipsize="end"
android:inputType="text"
/>
<Button
android:text="Hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:text="Hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
[编辑]尝试FunkTheMonk的方法不太有效,因为它最终会将中心按钮推出布局,如果字符串很长:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:text="Hello1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="center"
android:layout_weight="1"
>
<TextView
android:text="Testing a very long text to see how it works how do you like me now this is still longer yet"
android:layout_margin="3dp"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:singleLine="true"
android:gravity="center"
android:ellipsize="end"
android:inputType="text"
/>
<Button
android:text="Hello2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<Button
android:text="Hello3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
I'm trying to create a header with left and right buttons, and a title in the center, with a button immediately to the right of the button (which will allow selecting something). The layout should be like:
|<Button> <TextView><Button> <Button>|
If the title is very long, it should ellipsize. To do that, it appears the textview needs a layout_weight, as seen in this solution, otherwise it pushes the right buttons off the screen. However, setting the layout_weight for the textview pushes the center button off to the right:
|<Button> <TextView> <Button><Button>|
The layout as it stands is this:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:text="Hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:text="Testing"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:singleLine="true"
android:layout_weight="1"
android:gravity="center"
android:ellipsize="end"
android:inputType="text"
/>
<Button
android:text="Hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:text="Hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
[EDIT] Trying FunkTheMonk's approach doesn't quite work, as it ends up pushing the center button out of the layout if the string is long:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:text="Hello1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="center"
android:layout_weight="1"
>
<TextView
android:text="Testing a very long text to see how it works how do you like me now this is still longer yet"
android:layout_margin="3dp"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:singleLine="true"
android:gravity="center"
android:ellipsize="end"
android:inputType="text"
/>
<Button
android:text="Hello2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<Button
android:text="Hello3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不是 100% 确定你的意思。但我想我明白了。一种简单的方法是使用 tablerow 和 android:layout_marginLeft &正确的。
I'm not 100% sure what you mean. But i think i got it. One easy way to do it is to use a tablerow and android:layout_marginLeft & right.
在它们自己的水平线性布局中添加 TextView 和 Button,并设置其权重。将这个新的线性布局的重力设置为中心,文本视图和按钮将位于两个外部按钮之间的可用空间的中心。
如果标题很长,文本视图将在消失之前在线性布局中占据尽可能多的空间。
Add the TextView and Button in their own horizontal linear layout, and set the weight on that. Set this new linear layout's gravity to centre and the text view and button will be centred in the available space between the two outer buttons.
If the title is long, the text view will take up as much space as it can inside the linear layout before eclipsing.