Android开发系列(十八):自己定义控件样式在drawable目录下的XML实现

在Android开发的过程中,我们常常须要对控件的样式做一下改变,能够通过用添加背景图片的方式进行改变,可是背景图片放多了肯定会使得APK文件变的非常大。

我们能够用自己定义属性shape来实现。


shape:

gradient   -- 相应颜色渐变。 startcolor、endcolor就不多说了。 android:angle 是指从哪个角度開始变。

solid      --  填充。

stroke   --  描边。

corners  --  圆角。

padding   -- 定义内容离边界的距离。


我们先来看一下效果:




下边我们用详细的代码来演示一下:

首先,新建一个项目,然后我们在main.xml编辑:

<span style="font-size:18px;"><?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:orientation="vertical" >

  <TextView 
     	android:layout_width="fill_parent" 
		android:layout_height="wrap_content"
		android:layout_marginTop="10dp"
      	android:text="演示文本"
      	android:textSize="14pt"
      	android:textColor="#565656"
      	android:background="@drawable/bg_border"
      />
  <!-- 通过android:drawableLeft绘制一张图片 -->	
<TextView  
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="演示文本2"
	android:textSize="24pt"
	android:background="@drawable/bg_border2"
	/>

</LinearLayout></span>
能够看到,在上边的两个TextView的android:background属性,用到了@drawable/bg_border和@drawable/bg_border2这两个文件


然后,我们在@drawable目录下边创建这两个Android xml文件:

bg_border.xml:

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">    
	<!-- 填充:设置背景颜色 -->
	<solid android:color="#ffffff"/>
	<!-- 设置边框 -->
	<stroke  
	    android:width="5px"
	    android:color="#873600"
	    android:dashWidth="5dip"
	    />
	<!-- 定义内边距 -->
	<padding 
	    android:top="20dip"
	    />  
	    
	    
</shape></span>


bg_border2.xml:

<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!-- 指定圆角矩形的4个圆角的半径 -->
   	<corners android:topLeftRadius="20px"
    android:topRightRadius="5px"
    android:bottomRightRadius="20px"
    android:bottomLeftRadius="5px"/>
   	<!-- 指定边框线条的宽度和颜色 -->
	<stroke android:width="4px" android:color="#f0f" />
	<!-- 指定使用渐变背景色,使用sweep类型的渐变
	颜色从红色→绿色→蓝色 -->
	<gradient android:startColor="#f00"
	    android:centerColor="#0f0"
	    android:endColor="#00f"
	    android:type="sweep"/>
</shape></span>


我们看下我们的project文件夹视图:







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android中自定义Switch控件可以通过自定义drawable实现。以下是一个简单的例子: 1. 创建一个drawable资源文件,例如 switch_bg.xml,用于定义Switch的背景样式: ```xml <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:drawable="@drawable/switch_on_bg" /> <item android:state_checked="false" android:drawable="@drawable/switch_off_bg" /> </selector> ``` 2. 创建两个drawable资源文件,例如 switch_on_bg.xml 和 switch_off_bg.xml,分别用于定义Switch开和关状态下的样式。 switch_on_bg.xml: ```xml <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#00FF00" /> <corners android:radius="20dp" /> </shape> ``` switch_off_bg.xml: ```xml <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#FF0000" /> <corners android:radius="20dp" /> </shape> ``` 3. 在布局文件中使用自定义Switch控件: ```xml <Switch android:id="@+id/customSwitch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:thumb="@drawable/custom_switch_thumb" android:track="@drawable/switch_bg" /> ``` 其中,android:thumb属性定义了Switch的拇指(即开关按钮)的样式。我们可以创建一个自定义drawable实现: ```xml <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="#FFFFFF" /> <size android:width="20dp" android:height="20dp" /> </shape> ``` 这样就完成了自定义Switch控件样式。当Switch状态改变时,背景样式也会随之改变。你可以根据自己的需求来修改样式

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值