选中时为textView和editText添加边框形状,

Pou*_*ati 1 xml android shapes android-layout

我想为TextView和EditText创建边框形状,并在选择视图时显示它.

就像在这张照片中一样.

例

Nhấ*_*ang 6

您应该使用drawable选择器来实现您的UI.

首先创建一个background_edit_text_default.xml在用户未选择时为EditText的背景.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke
        android:width="1dp"
        android:color="#333D46" />

    <padding
        android:bottom="10dp"
        android:left="10dp"
        android:right="10dp"
        android:top="10dp" />
</shape>
Run Code Online (Sandbox Code Playgroud)

然后创建一个background_edit_text_selected.xml由用户选择时EditText的背景.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke
        android:width="1dp"
        android:color="#EDB90E" />

    <padding
        android:bottom="10dp"
        android:left="10dp"
        android:right="10dp"
        android:top="10dp" />
</shape>
Run Code Online (Sandbox Code Playgroud)

接下来创建一个background_edit_text.xml将用作EditText的背景.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/background_edit_text_default" android:state_focused="false" />
    <item android:drawable="@drawable/background_edit_text_selected" android:state_focused="true" />

</selector>
Run Code Online (Sandbox Code Playgroud)

最后background_edit_text.xml在布局文件中设置EditText的背景,例如activity_main.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/conteiner"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="20dp"
    android:orientation="vertical">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/background_edit_text" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="10dp" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/background_edit_text" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

你完成了,不需要在代码中添加任何东西.