如何创建一个EditText,顶角为圆角,底部为矩形

时间:2017-04-29 05:18:32

标签: android

我的drawable文件夹中有一个简单的背景,只是我在EditText上使用的边框:

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <corners
        android:radius="2dp"
        />
    <solid android:color="#ffffff"
        />
    <stroke
        android:width="1dip"
        android:color="#000" />
</shape>

但我希望实现this。 顶部圆形和边缘的底部矩形!我遇到了麻烦并且这样做了

4 个答案:

答案 0 :(得分:1)

创建一个Drawable形状xml文件

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

    <stroke
        android:width="1dip"
        android:color="@color/colorAccent"/>

    <corners android:radius="10dp"
        android:bottomLeftRadius="0dp"
        android:bottomRightRadius="0dp"/>

</shape>

在EditText背景中设置drawable

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Your Text"
    android:padding="@dimen/dimen10"
    android:background="@drawable/testing"/>

答案 1 :(得分:1)

创建 round_shape.xml

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

                <solid android:color="@color/white"/>
                <corners android:bottomRightRadius="0dp"
                    android:bottomLeftRadius="0dp"
                    android:topLeftRadius="20dp"
                    android:topRightRadius="20dp"/>

            </shape>

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

答案 2 :(得分:0)

请为我尝试这个工作     

<solid android:color="#ffffff" />

<stroke
    android:width="1dp"
    android:color="#000" />
<corners
    android:bottomLeftRadius="0dp"
    android:bottomRightRadius="0dp"
    android:topLeftRadius="25dp"
    android:topRightRadius="25dp" />

请检查并更改半径。

答案 3 :(得分:0)

这里创建editbox_custom_top_radius.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!--Here Corner use for give curve to your edit text-->
    <corners android:topLeftRadius="10dp" android:topRightRadius="10dp"/>

    <!--stroke is use to apply border on edit text-->
    <stroke android:width="2dp"/>

    <!--solid for incase you want background color to edit box-->
    <solid android:color="@color/app_grey"/>
</shape>

创建editbox_custom_bottom_radius.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!--Here Corner use for give curve to your edit text-->
    <corners android:bottomLeftRadius="10dp" android:bottomRightRadius="10dp"/>

    <!--stroke is use to apply border on edit text-->
    <stroke android:width="2dp"/>

    <!--solid for incase you want background color to edit box-->
    <solid android:color="@color/app_grey"/>
</shape>

并将其应用于您的编辑框

    <LinearLayout
     android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">


 <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Your Text"
        android:padding="@dimen/dimen10"
        android:background="@drawable/editbox_custom_top_radius"/>

<EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Your Text"
        android:padding="@dimen/dimen10"/>

<EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Your Text"
        android:padding="@dimen/dimen10"
        android:background="@drawable/editbox_custom_bottom_radius"/>

</LinearLayout>

试一试。

相关问题