Android自定义输入框样式

本文详细介绍了如何在Android中创建自定义ShapeDrawable资源文件,并应用于TextView背景及EditText光标颜色等场景,通过XML配置实现丰富的视觉效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

资料来自:菜鸟教程

自行编写一个ShapeDrawable的资源文件!然后TextView将blackgroung 设置为这个drawable资源即可!

shapeDrawable资源文件的几个节点以及属性:

  • <solid android:color = "xxx"> 这个是设置背景颜色的
  • <stroke android:width = "xdp" android:color="xxx"> 这个是设置边框的粗细,以及边框颜色的
  • <padding androidLbottom = "xdp"...> 这个是设置边距的
  • <corners android:topLeftRadius="10px"...> 这个是设置圆角的
  • <gradient> 这个是设置渐变色的,可选属性有: startColor:起始颜色 endColor:结束颜色 centerColor:中间颜色 angle:方向角度,等于0时,从左到右,然后逆时针方向转,当angle = 90度时从下往上 type:设置渐变的类型


一、自定义输入框列子:

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

  <gradient android:angle="45" />
    <!--设置边距-->
    <padding
        android:bottom="2dp"
        android:left="2dp"
        android:right="2dp"
        android:top="2dp" />
    <!-- 设置圆角矩形 -->
    <corners android:radius="3dp" />


    <!--设置边框粗细和颜色-->
    <stroke
        android:width="2px"
        android:color="#838B8B" />

    <!--<solid android:color="#838B8B" />-->

</shape>


二、在需要使用图文输入时,需要让drawableleft和文字有一个间隔的话:

android:drawablePadding="5dp"
android:drawableLeft="@drawable/phone"

需要改变光标的颜色:

1、自定义bg_cursor的drawable下xml文件:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <size android:width="1dp" />
    <solid android:color="#dbdbdb"  />
</shape>

2、在edittext下添加:

android:textCursorDrawable="@drawable/bg_cursor"

三、不自动获取焦点

在edittext的父级容器中添加属性:

android:focusable="true"
android:focusableInTouchMode="true"



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值