简体   繁体   English

在相对布局中创建布局

[英]create layout in relative layout

I need to create the layout like this but not getting perfect. 我需要创建这样的layout但不会变得完美。

getting this 得到这个

在此输入图像描述

need this way 需要这种方式

在此输入图像描述

I am creating this using RelativeLayout and need to create in this only. 我正在使用RelativeLayout创建它,并且只需要在此创建。 I am able to create with sub layout using Linearlayout but can this possible without using any sub layout 我可以使用Linearlayout创建子布局,但这可以不使用任何子布局

this is my code 这是我的代码

<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>

It seems your TextView and EditText aren't the same size. 看来你的TextViewEditText的大小不一样。 If you could make them the same size, they would probably align fine. 如果你可以使它们大小相同,它们可能会很好地对齐。

Providing the size of your views, yourbest bet is probably to stick with your sub LinearLayouts . 提供您的观点大小,最好的赌注可能是坚持您的子LinearLayouts

I'm not able to test it, but see if this might help: 我无法测试它,但看看这是否有帮助:

<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>

What I've done is as follows: align address_txt below address_title (instead of below title_add_txt ). 我所做的如下:在address_txt下面对齐address_title (而不是在title_add_txt下面)。 I've also changed the alignment of address to align to the top of address_txt and to the left of address_title (this might probably be solved in other ways too). 我也改变了排列address对齐到顶部address_txt和左address_title (这可能会以其他方式来解决太)。

Ad I said: I'm not able to verify it, but you can give it a try at least. 广告我说:我无法验证它,但你至少可以尝试一下。

layout_alignBaseline should solve your problem - it aligns text baselines of the views on the same line: 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>

I've also added margin between TextViews. 我还在TextViews之间添加了边距。 It looks like - I don't have your background image so EditTexts aren't aligned properly - if TextViews have the same width this won't be an issue: 它看起来像 - 我没有你的背景图像所以EditTexts没有正确对齐 - 如果TextViews具有相同的宽度,这将不是一个问题:

设备屏幕

You can play with margins of EditText and TextViews to add necessary space between them. 您可以使用EditText和TextViews的边距来添加它们之间的必要空间。

I would wrap the two related controls (label and corresponding input) in a LinearLayout and use android:layout_centerVertical="true" setting on the TextViews . 我会将两个相关控件(标签和相应的输入)包装在LinearLayout并在TextViews上使用android:layout_centerVertical="true"设置。

somthing like this... 这样的事......

<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