Android界面圆角输入框实现

最终效果如上图。

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="83dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:orientation="vertical" 
        android:background="@drawable/rounded_outer">

        <EditText
            android:id="@+id/namevalue"
            android:paddingTop="5dp"
            android:paddingLeft="5dp"
            android:paddingBottom="5dp"
            android:paddingRight="5dp"
            android:layout_marginLeft="1dp"
            android:layout_marginRight="1dp"
            android:layout_width="fill_parent"
            android:layout_height="38dp"
            android:layout_gravity="center"
            android:hint="用户名"
            android:inputType="text"
            android:textSize="20sp"
            android:background="@drawable/rounded_text">
        </EditText>
 
    	<View 
        	android:layout_width="fill_parent" 
        	android:layout_height="1dp" 
        	android:background="@color/light_grey" > 
    	</View>	
 
        <EditText
            android:id="@+id/passvalue"
            android:layout_width="fill_parent"
            android:layout_height="38dp"
            android:paddingTop="5dp"
            android:paddingLeft="5dp"
            android:paddingBottom="5dp"
            android:paddingRight="5dp"
            android:layout_marginLeft="1dp"
            android:layout_marginRight="1dp"
            android:layout_gravity="center"
            android:hint="密码"
            android:inputType="textPassword"
            android:textSize="20sp" 
            android:background="@drawable/rounded_text">
        </EditText>
    </LinearLayout>
</LinearLayout>

从上面布局文件可以看到,用到了rounded_outer 和 rounded_text 这两个XML文件。

rounded_outer.xml:

<?xml version="1.0" encoding="utf-8"?>  
<shape xmlns:android="http://schemas.android.com/apk/res/android"  
    android:shape="rectangle" 
    android:padding="3dp">  
    <!-- 填充 -->
    <solid android:color="@color/white" />
	<!-- 描边 -->
    <stroke android:width="1dp" android:color="@color/light_grey"/>
    <!-- 圆角 -->
    <corners android:bottomRightRadius="5dp"  
        android:bottomLeftRadius="5dp" 
        android:topLeftRadius="5dp"  
        android:topRightRadius="5dp" />  
</shape>  

rounded_text.xml:

<?xml version="1.0" encoding="utf-8"?>  
<shape xmlns:android="http://schemas.android.com/apk/res/android"  
    android:shape="rectangle" 
    android:padding="3dp">  
    <!-- 填充 -->
    <solid android:color="@color/white" />
	<!-- 描边 -->
    <stroke android:width="1dp" android:color="@color/light_grey"/>
    <!-- 圆角 -->
    <corners android:bottomRightRadius="5dp"  
        android:bottomLeftRadius="5dp" 
        android:topLeftRadius="5dp"  
        android:topRightRadius="5dp" />  
</shape>  

rounded_outer.xml 使整个布局为圆角的,当然它还有描边、填充色等属性设置。

rounded_text.xml 使圆角里面的文本框为圆角,与rounded_outer.xml的圆角匹配,在这两个xml文件中都设置圆角为5dp。

如果不用rounded_text.xml文件的话,效果如下:

明显看出“密码”框显示的问题。

在main.xml布局代码中有如下view:

<View  android:layout_width="fill_parent"        

            android:layout_height="1dp"        

            android:background="@color/light_grey" />
 它是用来分隔上下两个文本框的,若不加上这段代码,两个文本框之间就不会有一条连接的横线。

实现上述视图,用到的就是shape<形状控制>的使用,参看:android shape的使用。里面对于各属性介绍的很详细。


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

itzyjr

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值