android edittext align text to right and label to left

隐身守侯 提交于 2019-12-10 17:45:31

问题


I want to have an edit text where a label "Name" is displayed on left side of the edit text and the input is enterd from Right to left

If i set android:gravity as right , input is getting from right to left. But in this case the label ie whatever given as android:text for the edittext is also coming right aligned.

If i used android:hint , and set android:gravity as right, the hint is not displayed at all.

What is the option to get label on left and input on right.


回答1:


I don't know everything about android but still I am thinking this is not possible the way you are asking cause You want to set Gravity in 1 single EditText ,different for Different Words(name to left and text to right)

My idea to get the same result is,

1) Ask your designer to make you one White Round Corner Rectangle of Screen width and your desired height,and one another same image with Orange border (as you see while selecting any edittext)

2) Now ,In xml file take one <LinearLayout> ,set that first image as background to this layout and in that layout Take 1 TextView(for Name:) and EditText(to input) and set TextView - EditText background color to Transparent,ohh and yes give right gravity to EditText as you wished

3) Write one xml selector file for EditText background and instead of default orange,make focused bg to transparent.(so no one will recognise that this edittext is inside a layout when focused)

4) and write selector xml for LinearLayout and set focused background drawable to your second image.

And you will see on screen what you wanted.

Notes: If this is your client's design then Its Ok,If yours own then I am not in favour of you. and please don't ask me to write example.




回答2:


take a linear layout with horizontal orientation and add two Linear Layout with vertical orientation the first layout will have "Left gravity" and Name label and the other one will have Edit box and "Right Gravity".




回答3:


Use Table Layout

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="1" >

    <TableRow android:layout_marginTop="20dp" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/lblUser" />

        <EditText
            android:id="@+id/UserNameField"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="30dp"
            android:layout_x="68dp"
            android:layout_y="40dip"
            android:inputType="textEmailAddress"
            android:lines="1"
            android:maxLines="1"
            android:maxWidth="150dip"
            android:minWidth="230dip" >
        </EditText>
    </TableRow>

</TableLayout>


来源:https://stackoverflow.com/questions/8634948/android-edittext-align-text-to-right-and-label-to-left

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