android edittext align text to right and label to

2019-07-05 14:12发布

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.

3条回答
叛逆
2楼-- · 2019-07-05 14:37

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楼-- · 2019-07-05 14:48

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>
查看更多
Summer. ? 凉城
4楼-- · 2019-07-05 14:59

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.

查看更多
登录 后发表回答