【问题标题】:Relative layout with grouped vertical align具有分组垂直对齐的相对布局
【发布时间】:2012-08-27 10:24:41
【问题描述】:

我有相对布局。我如何进行分组垂直对齐:

【问题讨论】:

    标签: android view alignment android-relativelayout vertical-alignment


    【解决方案1】:

    我认为下面的代码会有所帮助..

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
    
         <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="70dp"
    android:orientation="horizontal">
    
    <TextView
        android:id = "@+id/Textview1"
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        tools:context=".TestActivity" />
    
    <EditText android:id="@+id/Editmessage1"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message" />
    
         </LinearLayout>
    
         <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="70dp"
    android:layout_alignLeft="@+id/LinearLayout1"
    android:layout_below="@+id/LinearLayout1"
    android:orientation="horizontal">
    
    
    <TextView
        android:id = "@+id/Textview2"
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        tools:context=".TestActivity" />
    
    <EditText android:id="@+id/Editmessage2"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message1" />
    
    
    
    </LinearLayout>
    
          </RelativeLayout>
    

    同样,添加另一个线性布局应该没问题。或者您可以将线性布局的方向垂直对齐,并将它们左右对齐。如果您使用的是 Eclipse Emulator,我认为每次更改 XML 代码时参考图形设计会更容易。

    有关对齐的更多信息,请参阅以下链接。

    http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html

    http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html

    您可以根据需要改变权重。使 textview 短而编辑文本长,反之亦然。

    【讨论】:

      【解决方案2】:

      谢谢,但我认为你的方式不适合我。我没有在图片上显示,但一些编辑文本可以有不止一个行高。 现在我做什么。我迭代所有文本视图,找到最大宽度并为它们设置这个宽度。

      【讨论】:

      • 如果您在每个 LinearLayout 的宽度中使用“wrap_content”,则无需这样做,并且解决方案更简单且更易于维护。
      • 是的,你说得对,但是如果布局有很多 textviews/edittexts,我们就会遇到问题。因为例如 1.5 对布局中的最大容器数(LinearLayout)有限制。
      猜你喜欢
      • 2015-08-21
      • 2012-06-25
      • 2013-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-08
      相关资源
      最近更新 更多