带有椭圆和按钮的 TextView

发布于 2024-12-17 01:34:19 字数 2880 浏览 2 评论 0原文

我正在尝试创建一个带有左右按钮的标题,标题位于中心,按钮右侧有一个按钮(这将允许选择某些内容)。布局应该是这样的:

|<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绅士风度i 2024-12-24 01:34:19

我不是 100% 确定你的意思。但我想我明白了。一种简单的方法是使用 tablerow 和 android:layout_marginLeft &正确的。

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TableRow android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello"
            android:layout_gravity="left|top"></Button>

        <TextView android:inputType="text" android:gravity="center"
            android:ellipsize="end" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="Testing"
            android:singleLine="true"
            android:layout_marginLeft="57dp"></TextView>
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:layout_gravity="center"
            android:text="Hello"
            android:layout_marginRight="57dp"></Button>


        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello"
            android:layout_gravity="right|top"></Button>
    </TableRow>

</LinearLayout>

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.

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TableRow android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello"
            android:layout_gravity="left|top"></Button>

        <TextView android:inputType="text" android:gravity="center"
            android:ellipsize="end" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="Testing"
            android:singleLine="true"
            android:layout_marginLeft="57dp"></TextView>
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:layout_gravity="center"
            android:text="Hello"
            android:layout_marginRight="57dp"></Button>


        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello"
            android:layout_gravity="right|top"></Button>
    </TableRow>

</LinearLayout>
没有心的人 2024-12-24 01:34:19

在它们自己的水平线性布局中添加 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文