问题
I want to set the line as background below EditText using shape xml but when I set it line set on center
I want like below

but when I set the shape below
drawable/line.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line" > <stroke android:height="4dp" android:color="@color/crabfx_dark_gray" /> </shape>
activity_main.xml
<EditText android:id="@+id/edt" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@android:color/black" android:layout_margin="20dp" android:background="@drawable/line"/>
I got below output

is any solution ?
回答1:
Try this one i hope you will get perfect solution..
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape>
<solid android:color="@color/gray" />
</shape>
</item>
<!-- main color -->
<item
android:bottom="1dp"
android:left="1dp"
android:right="1dp">
<shape>
<solid android:color="@color/white" />
</shape>
</item>
<!-- draw another block to cut-off the left and right bars -->
<item android:bottom="1dp">
<shape>
<solid android:color="@color/white" />
</shape>
</item>
</layer-list>
Or Try this
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape>
<solid android:color="@color/gray" />
</shape>
</item>
<item
android:bottom="1px"
android:left="0px">
<shape android:shape="rectangle" >
<solid android:color="#FFFFFF" />
</shape>
</item>
</layer-list>
回答2:
Try this way,hope this will help you to solve your problem.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="20dp">
<EditText
android:id="@+id/edt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/black"/>
<View
android:layout_width="match_parent"
android:layout_height="4dp"
android:background="@drawable/line"/>
<EditText
android:id="@+id/edt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/black"/>
<View
android:layout_width="match_parent"
android:layout_height="4dp"
android:background="@drawable/line"/>
</LinearLayout>
回答3:
Try this code just below your EDIT_text:
<View
android:id="@+id/dividerView"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#191919"
>
</View>
来源:https://stackoverflow.com/questions/25203292/how-to-set-line-as-a-background-using-xml-shape-below-to-edittext