【问题标题】:RelativeLayout Two TextViews Centered Above ButtonsRelativeLayout 两个 TextViews 在按钮上方居中
【发布时间】:2012-03-19 17:18:44
【问题描述】:

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

提前致谢。

【问题讨论】:

    标签: android textview android-relativelayout


    【解决方案1】:

    我刚刚尝试了两个级别的 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>
    

    【讨论】:

      【解决方案2】:

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

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

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

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

      【讨论】:

      • 谢谢,这是我最初的想法,但我希望使用RelativeLayouts,避免使用TableLayouts。
      猜你喜欢
      • 2012-10-17
      • 1970-01-01
      • 1970-01-01
      • 2016-07-18
      • 1970-01-01
      • 1970-01-01
      • 2018-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多