Android_RelativeLayout - How to put two elements on the same line?

前端 未结 5 1937
遇见更好的自我
遇见更好的自我 2021-01-25 05:32

The question is: I have a TextView and an EditText, how can I place them on the same \"level\"?

http://postimage.org/image/o9l17a3zv/

As you can see, I want to p

相关标签:
5条回答
  • 2021-01-25 06:01

    Use the android:layout_toRightOf or android:layout_toLeftOf attributes, exclusive to relative layouts

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="TextView" />
    
        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true" android:layout_toRightOf="@+id/textView1">
    
            <requestFocus />
        </EditText>
    
    </RelativeLayout>
    
    0 讨论(0)
  • 2021-01-25 06:03

    You have to center your text in the TextView widget for that just use the android attribute android:gravity in your TextView xml declaration.

    If you want Your text to get centered vertically and horizontally use:

     android:gravity="center"
    

    If you want Your text to get centered only horizontally use:

     android:gravity="center_horizontal"
    
    0 讨论(0)
  • 2021-01-25 06:04

    android:gravity="center" is the solution

    0 讨论(0)
  • 2021-01-25 06:06

    TEXT VIEW AND EDIT TEXT ARE ON THE SAME LINE TOUCHING THE BOTTOM LINE OF PARENT LAYOUT Try this one.

    android:layout_alignBottom="@+id/editText1"
    

    This will surely help you.

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/parentView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/editText1"
            android:text="TextView" />
    
        <EditText
            android:id="@+id/editText1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/textView1"
            android:ems="10" >
    
            <requestFocus />
        </EditText>
    
    </RelativeLayout>
    

    Thanks.

    0 讨论(0)
  • 2021-01-25 06:08

    try this

     <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    
    <TextView
        android:id="@+id/account_creation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="account_creation"
        android:textSize="20sp"
        android:textStyle="bold" />
    
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/account_creation" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="user_text" />
    
        <EditText
            android:id="@+id/editText1"
            android:hint="Enter_username"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_toRightOf="@+id/textView1" >
        </EditText>
    </RelativeLayout>
    
    </RelativeLayout>
    
    0 讨论(0)
提交回复
热议问题