relativeLayout 按钮上方居中的两个 TextView

发布于 2024-10-17 12:03:45 字数 211 浏览 5 评论 0原文

因此,在我的布局中,我在同一行上有两个按钮,一个左对齐,一个右对齐。我还在按钮上方设置了 TextView 作为标签,但它们也分别左对齐和右对齐。我希望它们位于按钮上方的中心并彼此在同一行,但如果不明确设置坐标,我无法弄清楚如何做到这一点,这会在某些手机上中断。我尝试设置各种权重和布局选项,但它只是无法按我想要的方式工作。有没有办法在相对布局中做到这一点?或者也许这是不可能的。

提前致谢。

So in my layout, I've got two buttons on the same line, one left aligned and one right aligned. I also have TextViews above the buttons to serve as labels, but they too are aligned left and right respectively. I'd like them to instead be centered above the buttons and on the same line as one another, but I can't figure out how to do this without explicitly setting coordinates, which will then break on some phones. I tried setting various weights and layout options, but it just doesn't work how I'd like. Is there a way to do this in a RelativeLayout? Or maybe it's just not possible.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

べ映画 2024-10-24 12:03:45

我刚刚尝试了两个级别的relativelayers,这是输出:
在此处输入图像描述

和代码:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <RelativeLayout
        android:id="@+id/relativeLayout2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/textView2"
            android:layout_centerHorizontal="true"
            android:text="Label longer than the button" >
        </TextView>

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/textView1"
            android:text="Button short" >
        </Button>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relativeLayout3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true" >

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/textView2"
            android:text="Button some longer" >
        </Button>

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="Test" >
        </TextView>
    </RelativeLayout>
</RelativeLayout>

I've just tried with two levels of RelativeLayers, and here is the output:
enter image description here

and the code:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <RelativeLayout
        android:id="@+id/relativeLayout2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/textView2"
            android:layout_centerHorizontal="true"
            android:text="Label longer than the button" >
        </TextView>

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/textView1"
            android:text="Button short" >
        </Button>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relativeLayout3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true" >

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/textView2"
            android:text="Button some longer" >
        </Button>

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="Test" >
        </TextView>
    </RelativeLayout>
</RelativeLayout>
蓦然回首 2024-10-24 12:03:45

创建一个包含 2 列和 2 行的表格布局。

使第一个和最后一个列可收缩,中间列可拉伸。

|----|-------------------|-----|
|  1 |                   |  2  |
|----|-------------------|-----|
|  3 |                   |  4  |
|----|-------------------|-----|

将标签放在 1 和 2 处,将按钮放在 3 和 4 处。
然后将所有列内容居中。

Create a tablelayout with 2 columns and 2 rows.

Make first and last columnn shrinkable, and middle column stretchable.

|----|-------------------|-----|
|  1 |                   |  2  |
|----|-------------------|-----|
|  3 |                   |  4  |
|----|-------------------|-----|

Put your labels at 1 and 2, and buttons at 3 and 4.
Then center all column content.

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