简体   繁体   中英

How to divide text in EditBox as TextView and EditBox in Android Layout

I want my EditText to look like these - 在此处输入图片说明 I am able to achieve the EditText but I am not able to keep a TextView aside it.

here is my code -

<LinearLayout
        android:id="@+id/input_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginLeft="25dp"
        android:layout_marginRight="25dp"
        android:layout_marginBottom="50dp"
        android:layout_alignParentBottom="true"
        android:gravity="center">

    <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:textColor="@android:color/white"
            android:hint="Username"
            android:background="@drawable/rounded_edit_text" />

    <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:textColor="@android:color/white"
            android:hint="Password"
            android:background="@drawable/rounded_edit_text" />
</LinearLayout>

Try this

<LinearLayout
        android:layout_width="250dp"
        android:layout_height="40dp"
        android:background="@drawable/linear_bg"
        android:orientation="horizontal">

    <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".4"
            android:gravity="center"
            android:padding="5dp"
            android:text="UserName"
            android:textColor="@android:color/white" />

    <EditText
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".6"
            android:background="@android:color/transparent"
            android:gravity="center"
            android:hint="******"
            android:padding="5dp"
            android:textColor="@android:color/white"
            android:textColorHint="@android:color/white" />

</LinearLayout>

drawable/linear_bg

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
    <solid android:color="#3F51B5" />
    <corners android:radius="20dp" />
</shape>

OUTPUT

在此处输入图片说明

OUTPUT in device

You should have the TextView and EditText in a horizontal LinearLayout. The background should be applied to the LinearLayout instead of EditText

  <LinearLayout
        android:id="@+id/input_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="@dimen/form_margin"
        android:padding="8dp"
        android:background="@drawable/background"
        android:layout_alignParentBottom="true"
        android:gravity="center">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:textSize="20sp"
            android:textColor="@android:color/white"
            android:text="username"
             />

        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:layout_marginLeft="16dp"
            android:textColor="@android:color/white"
            android:textSize="20sp"
            android:text="Olivia Gu" />
    </LinearLayout>

在此处输入图片说明

background -

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="@color/colorPrimary" />
    <stroke android:width="1dip"
        android:color="@color/colorAccent" />
    <corners android:radius="8dp" />
</shape>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM