如何让Android的RelativeLayout正常工作?

时间:2016-06-03 15:27:22

标签: android xml android-layout

标题有点挑衅,但我观察到一些对我来说无法解释的行为。

我有三组视图,每组有一个TextView tvX和一个Spinner spX,X = A,B,C。 textview是微调器的标题或提示,所以我希望Spinner layout_toRightOf它是相应的TextView。由于它们互相引用,我还将layout_alignBaseline Spinner转换为TextView。这工作正常。现在我希望堆叠的三个集合使得TextViews具有对齐的右边缘(每个都有一个尾部冒号,这些应该对齐),并且Spinners已经对齐了左边缘。

中的堆栈
  +----------+----------+
  |      tvA:|spA       |
  +----------+----------+
  |      tvB:|spB       |
  +----------+----------+
  |      tvC:|spC       |
  +----------+----------+

B和C视图分别获得引用A和B的属性;即layout_below和layout_alignLeft / Right。

现在,tvB和tvC的文本比tvA更长。我猜想RelativeLayout会缩小tvA,这样就可以为tvB和tvC的较长文本留出空间,这样它们就是右对齐的。相反的是,整个左栏获得了tvA文本的宽度,而tvB和tvC分别被分为两行。我试图重新安排一些东西,使tvB成为主播,并且layout_above tvA。这导致tvA完全消失。无论如何,它不是一个合适的解决方案,因为在另一种语言中,文本的相对长度可能是另一种方式!如果我为tvA添加字符以使其最长,tvB和tvC不会再被破坏,但冒号仍然没有对齐!事实上,看起来tvB在tvA下面居中,而tvC集中在tvC之下。

如果我左对齐tvX并右对齐spX,它看起来最好。但即使在那种情况下也会发生一些奇怪的事情:Spinners不服从属性layout_width =" wrap_content"它们不再扩展,以填充TextViews留下的所有空间。中间的粗糙对齐看起来仍然很糟糕,这就是为什么我希望在中间发生对齐的原因。

Android Studio在编辑XML时显示生成的布局,如果光标位于View的XML定义中,它还会显示Views边缘。在那里,我可以很好地观察属性中的布局提示如何被忽略!在实际的手机上也会发生同样的事情。

以下是XML代码:

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="@dimen/indented_margin"
        android:layout_marginBottom="5dp">

        <TextView
            android:id="@+id/device_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:text="@string/settings_sb_device" />

        <Spinner
            android:id="@+id/device"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:spinnerMode="dropdown"
            android:layout_toRightOf="@id/device_title"
            android:layout_alignBaseline="@id/device_title" />

        <TextView
            android:id="@+id/can_speed_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:layout_below="@id/device_title"
            android:layout_alignRight="@id/device_title"
            android:text="@string/settings_sb_speed" />

        <Spinner
            android:id="@+id/can_speed"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:spinnerMode="dropdown"
            android:layout_toRightOf="@id/can_speed_title"
            android:layout_alignBaseline="@id/can_speed_title"
            android:layout_alignLeft="@id/device"
            android:entries="@array/can_speeds" />

        <TextView
            android:id="@+id/baudrate_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:layout_below="@id/can_speed_title"
            android:layout_alignRight="@id/can_speed_title"
            android:text="@string/settings_sb_baudrate" />

        <Spinner
            android:id="@+id/baudrate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:spinnerMode="dropdown"
            android:layout_toRightOf="@id/baudrate_title"
            android:layout_alignBaseline="@id/baudrate_title"
            android:layout_alignLeft="@id/can_speed"
            android:entries="@array/baudrates" />
    </RelativeLayout>

我搜索了相关视图的可用属性集,但无法找到线索。任何人都可以解释这种行为吗?我忽略了什么?

2 个答案:

答案 0 :(得分:1)

对于columnCount为2

,请使用GridLayout

例如:

<android.support.v7.widget.GridLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                app:alignmentMode="alignBounds"
                app:columnCount="2"
                app:orientation="vertical"
                >

对于左侧Tv +冒号,使用RelativeLayout甚至LinearLayout都可以解决此问题。

              <LinearLayout
                    android:gravity="center"
                    android:orientation="horizontal"
                    app:layout_column="0"
                    app:layout_columnWeight="1"
                    app:layout_gravity="fill_horizontal|fill_vertical"
                    app:layout_row="0">

                    <TextView 
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        tools:text="Label"
                        android:maxLines="2"
                        android:ellipsize="end"
                        android:textColor="@color/black"/>

                   <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        tools:text=":"
                        android:textColor="@color/black"/>
           </LinearLayout>

答案 1 :(得分:0)

在我看来,GridLayout可以更好地满足您的需求......