自定义控件View
创建一个类继承我们需要的控件 (如: textview,linearLayout…..等等) , 实现其中的三个构造函数
public class CustomerTextView extends TextView {
public HomeTextView(Context context) {
this(context, null);
}
//在布局文件中使用的时候调用的方法
//布局控件最终都会通过反射转化成相应的代码来实现,控件中的属性都会反射到AttributeSet参数中保存
//通过AttributeSet获取出布局文件中控件设置的属性
public HomeTextView(Context context, AttributeSet attrs) {
//super(context, attrs);
this(context, attrs, 0);
}
//在代码内部调用
//defStyle : textview样式,默认样式
public HomeTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
/**
*android:singleLine="true"
*android:ellipsize="marquee"
*android:focusableInTouchMode="true"
*android:focusable="true"
*android:marqueeRepeatLimit="marquee_forever"
*/
// 通过代码设置的属性
setSingleLine();
setEllipsize(TruncateAt.MARQUEE);
setFocusableInTouchMode(true);
setFocusable(true);
setMarqueeRepeatLimit(-1);
}
}
这就是一个具有我们想要的自定义Textview
自定义控件的使用(与系统控件无异同)
<控件所在的包名.CustomerTextView>
</控件所在的包名.CustomerTextView>
自定义属性
在res/values下创建一个attrs.xml文件,在该文件中创建该自定义属性
1.书写自定义属性的格式:
<resources>
<declare-styleable name="SettingView">
<attr name="title" format="string"/> <!-- format : 属性的值的类型 可以设置属性 在attr中 -->
<attr name="">
<enum name="" value=""/>
<enum name="" value=""/>
</attr>
</declare-styleable>
</resources>
2. 自定义属性的使用:
设置命名空间 :
mlns:app="http://schemas.android.com/apk/res/我们工程的包名(表示在我们的工程中有效,离开了就无效了)" 老的写法是这样
xmlns:app="http://schemas.android.com/apk/res-auto" --- 新写法统一写成res-auto
xmlns:android= "http://schemas.android.com/apk/res/android" --android系统写法
自定义属性的使用:
app:title=""
android:title=""
3. 在布局文件设置自定义属性后 , 自定义属性必须在自定义控件中使用之后才有效
1> 在自定义控件的三个参数构造函数中获取属性的值
//AttributeSet保存有控件的所有属性的值,通过AttributeSet获取自定义属性的值
//获取在attrs.xml中设置的相应的控件的属性的值的集合
TypedArray mTa = context.obtainStyledAttributes(attrs, R.styleable.SettingView);
//从集合中获取特定属性的值
String mTitle = mTa.getString(R.styleable.SettingView_title);
2> 初始化自定义控件中的控件
//初始化控件
mSettingViewTitle = (TextView) view.findViewById(R.id.tv_settingview_title);
3> 将获取的属性的值设置给初始化出来的控件
//将获取的属性的值设置给相应的控件进行显示了
mSettingViewTitle.setText(mTitle);
//释放资源
mTa.recycle();
相同样式的抽取
查看系统的样式书写格式 , 在res/values/styles.xml文件中
将组件的共同的属性抽取出来,放在styles.xml文件中 ( 继承某个组件之后就可以使用它的所有样式, 如果在该样式中再次设置,将会覆盖上面的属性,即就是上面的无效)
书写格式如下:
<style name="自定义一个名" parent="继承自那个组件">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
...
</style>
样式抽取的使用:
<TextView
style="@style/自己定义的那个名"
android:text="设置中心"/>
状态选择器:
一般有状态改变的都会使用状态选择器, 状态改变: 如 按下 , 抬起 , 悬浮......
查看源码 : \adt-bundle-windows-x86_64_20140101\sdk\platforms\android-15\data\res\drawable
创建状态选择器 : 在 res 下创建一个drawable 目录 ,在其中书写相关的 xml 文件
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 非正常情况下 -->
<item android:drawable="@drawable/antispam_report_button_press"
android:state_pressed="true"/>
<!-- 正常情况下 -->
<item android:drawable="@drawable/antispam_report_button"/>
</selector>
自定义图片 (使用xml文件自定义一些图片)
同上面的状态选择器一样 ,在创建的drawable中 , 创建一个xml文件 , < 选择 shape 标签>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<!-- corners : 弧度半径 -->
<corners android:radius="16dp" />
<!-- size : 大小 -->
<size android:width="32dp"
android:height="32dp"/>
<!-- solid : 颜色 -->
<solid android:color="#99ffffff"/>
</shape>
Shape基本属性(corners、gradient、padding、size、solid、stroke)
样式属性:rectangle (矩形)、oval(椭圆)、line(线形)、ring(环形)
1> Corners 标签是用来字义圆角的,其中radius与其它四个并不能共同使用。
<corners //定义圆角
android:radius="dimension" //全部的圆角半径
android:topLeftRadius="dimension" //左上角的圆角半径
android:topRightRadius="dimension" //右上角的圆角半径
android:bottomLeftRadius="dimension" //左下角的圆角半径
android:bottomRightRadius="dimension" /> //右下角的圆角半径
2、solid 用以指定内部填充色
<solid android:color="color" />
3、gradient用以定义渐变色,可以定义两色渐变和三色渐变,及渐变样式(首先有三种渐变类型,分别是:linear(线性渐变)、radial(放射性渐变)、sweep(扫描式渐变))
<gradient
android:type=["linear" | "radial" | "sweep"] //共有3中渐变类型,线性渐变(默认)/放射渐变/扫描式渐变 android:angle="integer" //渐变角度,必须为45的倍数,0为从左到右,90为从上到下
android:centerX="float" //渐变中心X的相当位置,范围为0~1 android:centerY="float" //渐变中心Y的相当位置,范围为0~1
android:startColor="color" //渐变开始点的颜色
android:centerColor="color" //渐变中间点的颜色,在开始与结束点之间
android:endColor="color" //渐变结束点的颜色
android:gradientRadius="float" //渐变的半径,只有当渐变类型为radial时才能使用
android:useLevel=["true" | "false"] /> //使用LevelListDrawable时就要设置为true。设为false时才有渐变效果
4、stroke 这是描边属性,可以定义描边的宽度,颜色,虚实线等
<stroke
android:width="dimension" //描边的宽度
android:color="color" //描边的颜色
// 以下两个属性设置虚线
android:dashWidth="dimension" //虚线的宽度,值为0时是实线
android:dashGap="dimension" /> //虚线的间隔
5、size和padding 这两个基本上不怎么用,因为他们所具有的功能,控件本身也能实现。
size:是用来定义图形的大小的
<size
android:width="dimension"
android:height="dimension" />
padding:用来定义内部边距
<padding
android:left="dimension"
android:top="dimension"
android:right="dimension"
android:bottom="dimension" />
自定义图片之layer-list
自定义带边框的图片(例如:顶部需要边框或者其他部位需要边框),则就需要使用layer-list来进行画图(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="#CCCDD3"/>
</shape>
</item>
<!-- 主体背景颜色值,即相当于第二张图片(让第二张图片指定部位的距离第一张图片指定部位距离多
少)-->
<item
android:bottom="0.5dp"
android:left="0.5dp"
android:right="0.5dp">
<shape>
<solid android:color="#ffffff"/>
<padding
android:bottom="0dp"
android:left="0dp"
android:right="0dp"
android:top="0dp"/>
</shape>
</item>
</layer-list>
使用 layer-list自定义状态选择的图片且自定阴影
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 按下时的图片 -->
<item android:state_pressed="true">
<!-- 灰色阴影 -->
<layer-list>
<item
android:left="2dp"
android:top="4dp">
<shape>
<solid android:color="@android:color/darker_gray" />
<corners android:radius="4dp" />
</shape>
</item>
<!-- 红色前景 -->
<item
android:bottom="4dp"
android:right="2dp">
<shape>
<solid android:color="#FF0000" />
<corners android:radius="4dp" />
</shape>
</item>
</layer-list>
</item>
<!-- 正常情况下的图片 -->
<item>
<!-- 灰色阴影 -->
<layer-list>
<item
android:left="2dp"
android:top="4dp">
<shape>
<solid android:color="@android:color/darker_gray" />
<corners android:radius="4dp" />
</shape>
</item>
<!-- 白色前景 -->
<item
android:bottom="4dp"
android:right="2dp">
<shape>
<solid android:color="#FFFFFF" />
<corners android:radius="4dp" />
</shape>
</item>
</layer-list>
</item>
</selector>
这是我对自定义控件,自定义view,自定义drawable,状态选择器以及shape的简单理解,希望读者有所帮助……