如何使用xml(形状)将线设置为EditText的背景?

3
我想使用形状xml将线设置为EditText下方的背景,但是当我设置它时,线会居中显示。我想要如下所示的效果: enter image description here
但是当我设置以下形状时:
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"/>
我得到了以下输出 enter image description here 有解决方案吗?
3个回答

7
尝试这个,我希望你能得到完美的解决方案...
<?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>

或者尝试这个。
<?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>

0

尝试这种方式,希望能帮助您解决问题。

<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>

我不需要这个解决方案,我需要形状。 - Jayesh

-1

在你的EDIT_text下面尝试这段代码:

<View
    android:id="@+id/dividerView"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#191919"
     >
</View>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接