繁体   English   中英

在相对布局中创建布局

[英]create layout in relative layout

我需要创建这样的layout但不会变得完美。

得到这个

在此输入图像描述

需要这种方式

在此输入图像描述

我正在使用RelativeLayout创建它,并且只需要在此创建。 我可以使用Linearlayout创建子布局,但这可以不使用任何子布局

这是我的代码

<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="15dp">
      <TextView android:text="Title" android:textColor="#ffffff" android:layout_width="wrap_content"
          android:layout_height="wrap_content" android:textSize="18dp" android:padding="5dp"
          android:background="@drawable/tab_cyan" android:id="@+id/title_add_txt"/>
      <EditText android:hint="Address Title" android:layout_width="match_parent" android:layout_height="wrap_content"
           android:id="@+id/address_title" android:layout_toRightOf="@id/title_add_txt"/>
       <TextView android:text="Address" android:textColor="#ffffffff" android:layout_width="wrap_content"
           android:layout_height="wrap_content" android:textSize="18dp" android:padding="5dp"
           android:background="@drawable/tab_cyan" android:layout_below="@id/title_add_txt"
           android:id="@+id/address_txt"/>
       <EditText android:hint="Address" android:layout_width="match_parent" android:layout_height="wrap_content"
           android:id="@+id/address" android:layout_below="@id/address_title" android:layout_toRightOf="@id/address_txt"/>
</RelativeLayout>

看来你的TextViewEditText的大小不一样。 如果你可以使它们大小相同,它们可能会很好地对齐。

提供您的观点大小,最好的赌注可能是坚持您的子LinearLayouts

我无法测试它,但看看这是否有帮助:

<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="15dp">
    <TextView android:text="Title" android:textColor="#ffffff" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:textSize="18dp" android:padding="5dp"
        android:background="@drawable/tab_cyan" android:id="@+id/title_add_txt"/>
    <EditText android:hint="Address Title" android:layout_width="match_parent" android:layout_height="wrap_content"
        android:id="@+id/address_title" android:layout_toRightOf="@id/title_add_txt"/>
    <TextView android:text="Address" android:textColor="#ffffffff" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:textSize="18dp" android:padding="5dp"
        android:background="@drawable/tab_cyan" android:layout_below="@id/address_title"
        android:id="@+id/address_txt"/>
    <EditText android:hint="Address" android:layout_width="match_parent" android:layout_height="wrap_content"
        android:id="@+id/address" android:layout_alignTop="@id/address_txt" android:layout_alignLeft="@id/address_title"/>
</RelativeLayout>

我所做的如下:在address_txt下面对齐address_title (而不是在title_add_txt下面)。 我也改变了排列address对齐到顶部address_txt和左address_title (这可能会以其他方式来解决太)。

广告我说:我无法验证它,但你至少可以尝试一下。

layout_alignBaseline应解决您的问题 - 它在同一行上对齐视图的文本基线:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="15dp" >

    <TextView
        android:id="@+id/title_add_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#5500ff00"
        android:padding="5dp"
        android:text="Title"
        android:textColor="#ffffff"
        android:textSize="18dp" />

    <EditText
        android:id="@+id/address_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/title_add_txt"
        android:layout_alignBaseline="@id/title_add_txt"
        android:hint="Address Title" />

    <TextView
        android:id="@+id/address_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/title_add_txt"
        android:layout_marginTop="10dp"
        android:background="#5500ff00"
        android:padding="5dp"
        android:text="Address"
        android:textColor="#ffffffff"
        android:textSize="18dp" />

    <EditText
        android:id="@+id/address"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/address_title"
        android:layout_toRightOf="@id/address_txt"
        android:layout_alignBaseline="@id/address_txt"
        android:hint="Address" />
</RelativeLayout>

我还在TextViews之间添加了边距。 它看起来像 - 我没有你的背景图像所以EditTexts没有正确对齐 - 如果TextViews具有相同的宽度,这将不是一个问题:

设备屏幕

您可以使用EditText和TextViews的边距来添加它们之间的必要空间。

我会将两个相关控件(标签和相应的输入)包装在LinearLayout并在TextViews上使用android:layout_centerVertical="true"设置。

这样的事......

<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="15dp">
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

      <TextView android:text="Title" android:textColor="#ffffff" android:layout_width="wrap_content"
          android:layout_height="wrap_content" android:textSize="18dp" android:padding="5dp"
          android:background="@drawable/tab_cyan" android:id="@+id/title_add_txt"
          android:layout_centerVertical="true" />
      <EditText android:hint="Address Title" android:layout_width="match_parent" android:layout_height="wrap_content"
           android:id="@+id/address_title" android:layout_toRightOf="@id/title_add_txt"/>
   </LinearLayout>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
       <TextView android:text="Address" android:textColor="#ffffffff" android:layout_width="wrap_content"
           android:layout_height="wrap_content" android:textSize="18dp" android:padding="5dp"
           android:background="@drawable/tab_cyan" android:layout_below="@id/title_add_txt"
           android:id="@+id/address_txt"
           android:layout_centerVertical="true"/>
       <EditText android:hint="Address" android:layout_width="match_parent" android:layout_height="wrap_content"
           android:id="@+id/address" android:layout_below="@id/address_title" android:layout_toRightOf="@id/address_txt"/>
   </LinearLayout>
</RelativeLayout>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM