选择后,为textView和editText添加边框形状, [英] when selected add border shape for textView and editText ,
问题描述
我想为TextView和EditText创建边框形状,并在选择视图时显示它.
I want to create a border shape for TextView and EditText, and show it when a view is selected.
就像这张照片一样.
推荐答案
您应该使用drawable选择器来实现UI.
You should use drawable selector to achieve your UI.
首先创建一个background_edit_text_default.xml
,它是用户未选择EditText时的背景.
First create a background_edit_text_default.xml
which is the background of EditText when it is not selected by users.
<?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>
然后创建一个background_edit_text_selected.xml
,它是用户选择EditText时的背景.
Then create a background_edit_text_selected.xml
which is the background of EditText when it is selected by users.
<?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>
下一步创建一个background_edit_text.xml
,它将用作EditText的背景.
Next create a background_edit_text.xml
which will be used as background of the 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>
最后将background_edit_text.xml
设置为布局文件(例如activity_main
)中EditText的背景.
Finally set background_edit_text.xml
as background of your EditText in your layout file, such as 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>
您已完成,无需在代码中添加任何内容.
You're done and no need to add anything in code.
这篇关于选择后,为textView和editText添加边框形状,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!