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

只谈情不闲聊 提交于 2019-12-20 04:33:24

问题


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 put "Username" on the same level of the EditText...

Here my code:

<TextView
    android:id="@+id/account_creation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/account_creation"
    android:textSize="20sp"
    android:textStyle="bold" />

<!-- Username -->

<TextView
    android:id="@+id/username_registration"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@id/account_creation"
    android:layout_toLeftOf="@+id/username_new_account"
    android:text="@string/user_text"
    android:textSize="15sp" />

<EditText
    android:id="@+id/username_new_account"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/account_creation"
    android:layout_alignParentRight="true"
    android:ems="10"
    android:hint="@string/user_editext" >

    <requestFocus />
</EditText>

I was thinking to put the same value of "height" on both of them, but i don't know if it's the right/better way...


回答1:


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.




回答2:


android:gravity="center" is the solution




回答3:


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>



回答4:


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>



回答5:


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"


来源:https://stackoverflow.com/questions/11577570/android-relativelayout-how-to-put-two-elements-on-the-same-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!