自定义TextView
1.在布局文件中写好一个TextView组件
<TextView
android:id="@+id/tv_paomadeng"
android:layout_width="match_parent"
android:layout_height="20dp"
android:text="滚动广告牌"
android:singleLine="true" //单行显示
android:ellipsize="marquee" //滚动
/>
2.创建一个TextView的子类,重写其中的方法,加上我们的修改需求。
/**
* 自定义一个view,当系统提供的组件无法满足需要时,可以写一个子类继承父类组件,修改他的属性,达到实现的效果。
* @author watom_Thinkpad
*
*/
public class FocusedTextView extends TextView {
public FocusedTextView(Context context) {
super(context);
}
public FocusedTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FocusedTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean isFocused() {
// 强制让系统检测到TextView有焦点
return !super.isFocused();
}
}
3.修改布局文件中TextView的默认的使用标签,把默认的标签修改成自定义的TextView全路径标签。
<com.watom.mobilesafe.view.FocusedTextView
android:id="@+id/tv_paomadeng"
android:layout_width="match_parent"
android:layout_height="20dp"
android:text="滚动广告牌"
android:singleLine="true"
android:ellipsize="marquee"
/>
4.View组件获得焦点:
<TextView
android:id="@+id/tv_paomadeng"
android:layout_width="match_parent"
android:layout_height="20dp"
android:ellipsize="marquee"
android:singleLine="true" <!--View组件单行显示-->
android:text="滚动广告牌"
**android:focusable="true"** <!--View组件获得焦点-->
android:focusableInTouchMode="true"
/>
抽取样式或主题
使用场景:当一个view组件(如TextView)需要经常使用时,它的样式除了一小部分需要改变,其余的大部分一直不变,我们就有必要抽取一个样式的模板。
<TextView
android:id="@+id/tv_name" <!--每个页面不同,所以这个属性不用抽取-->
android:layout_width="match_parent"
android:layout_height="30dp"
android:gravity="center"
android:text="应用名称" <!--每个页面不同,所以这个属性不用抽取-->
android:textSize="18dp"
抽取步骤:
1. 在res/values目录下打开styles.xml,添加需要抽取的样式。
<!-- 标题栏样式 -->
<style name="TitleStyle"> <!--自定义name,在下面使用时引用-->
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">30dp</item>
<item name="android:gravity">center</item>
<item name="android:textSize">18dp</item>
<item name="android:background">#99ff33</item>
</style>
使用步骤:
1.其他界面需要这个样式时,在布局文件中按照下面的格式写,在Style.xml中找到对应样式的name,按照 style=”@style/TitleStyle”格式引用,并添加需要的属性。
<TextView
style="@style/TitleStyle"
android:id="@+id/tv_gongnengList"
android:text="功能列表"
/>
自定义组合控件:
组合控件,其实就是针对ViewGroup可以放进去许多其他组件的布局,如RelativeLayout、LinearLayout等都可以放进去TextView、CheckBox和其他的组件,因为他们继承了ViewGroup,但TextView是不能放进去其他的组件,只能修改。
使用场景:当几个组件组合成的一个布局文件,这个布局在其他地方大量复用时,可以把这个布局抽取成一个组合控件。
下面以RelativeLayout 举例。
1. 单独创建一个XML布局文件,把需要抽取的布局放进这个布局文件中。
布局文件名:item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp" >
<TextView
android:id="@+id/tv_setting_tile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标题"
android:textSize="18dp"/>
<TextView
android:id="@+id/tv_setting_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_setting_tile"
android:layout_marginTop="4dp"
android:text="描述状态" />
<CheckBox
android:id="@+id/cb_setting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="开启" /><!--text是在CheckBox后面添加文字-->
<!--添加了一条横线-->
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000"
android:layout_marginBottom="1dp"
android:layout_below="@id/tv_setting_name"/>
</RelativeLayout>
- 把上面的item.xml布局添加到ViewGroup的子类中。创建一个子类来继承RelativeLayout,实现以下父类中的构造方法,关键是添加需求:把抽取出来的样式填充到子类中。
子类的全路径:com.watom.mobilesafe.view.SettingItemView
public class SettingItemView extends RelativeLayout {
private static final String NAMESPACE = "http://schemas.android.com/apk/res/com.watom.mobilesafe";
private TextView tvTitle;
private TextView tvDesc;
private CheckBox cbCheck;
public SettingItemView(Context context) {
super(context);
initView();
}
public SettingItemView(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public SettingItemView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
/**
*初始化自定义控件布局,initView一般是私有化
*/
private void initView(){
View view = View.inflate(getContext(), R.layout.item, null);
//把写好的item.xml布局,填充到RelativeLayout子类中
this.addView(view);
//在这里通过view对象找到item布局中的所有组件,方便以后填充数据
tvTitle = (TextView) view.findViewById(R.id.tv_setting_tile);
tvDesc = (TextView) view.findViewById(R.id.tv_setting_name);
cbCheck = (CheckBox) view.findViewById(R.id.cb_check);
}
/**
* 暴露调用私有属性的方法
* 设置标题
*/
public void setTitle(String title) {
tvTitle.setText(title);
}
/**
* 设置描述
*/
public void setDesc(String desc) {
tvDesc.setText(desc);
}
/**
* 获取当前checkbox的选中状态。
* API提供方法cbCheck.isChecked()能检测到自己是否被选中。
* 选中--true 未选中--false
*/
public boolean isChecked() {
return cbCheck.isChecked();
}
/**
* 设置checkbox选中状态,并用setDesc方法更改tvDesc组件的内容;
*/
public void setChecked(boolean isChecked) {
cbCheck.setChecked(isChecked);
if (isChecked) {
setDesc(mDescOn);
} else {
setDesc(mDescOff);
}
}
}
使用步骤:
1. 需要引用这个布局时,把自定义的ViewGroup**子类的全路径**添加到标签中。
注意使用后由于是自定义的布局,布局无法及时画出来,运行起来后会显示正常。
<com.watom.mobilesafe.view.SettingItemView
android:id="@+id/item_custom
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
2.在需要使用这个布局文件的地方,用findViewById找到这个控件,来给这个控件的每个组件设置/填充数据。
SettingItemView sivUpdate = (SettingItemView) findViewById(R.id.item_custom);
4.添加相关方法和自定义属性
自定义属性:
使用场景:在自定义控件完成后,在代码中我们需要根据业务逻辑不断的来更新组合控件中的某个组件的状态,是整个业务逻辑很乱,我们需要把这些组件的各种状态编辑成他们的属性,
1. 在…sdk\platforms\android-16\data\res\values路径下打开attrs.xml文件,仿照其中的标签和属性来写自定义的属性。
- 自定义的属性文件。在自己的项目中,/自己的项目名称/res/values下新建一个attrs.xml,仿照格式如下,注意:name是整数类型时,不用写format。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SettingItemView"><!--自定义name,-->
<attr name="title" format="string" />
<attr name="desc_on" format="string" />
<attr name="desc_off" format="string" />
</declare-styleable>
</resources>
3.自定义的布局文件中,声明自定义命名空间和属性。
(1) android:layout_width=”match_parent” 由于已经声明了安卓系统的命名空间,所以可以使用系统提供的属性( android:XXX)。xmlns:android=”http://schemas.android.com/apk/res/android”
(2)自定义的组合控件中也可以使用自定义的属性,但是其属性必须自己定义,xmlns:watom=”http://schemas.android.com/apk/res/包名”
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:watom="http://schemas.android.com/apk/res/com.watom.mobilesafe"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
style="@style/TitleStyle"
android:text="设置中心" />
<com.watom.mobilesafe.view.SettingItemView
android:id="@+id/siv_update"
android:layout_width="match_parent"
android:layout_height="wrap_content"
watom:title="自动更新设置"
watom:desc_on="更新提示已开启"
watom:desc_off="更新提示已关闭"
/>
自定义属性写完了
使用步骤:
4.在自定义控件的类构造方法中,getAttributeValue获取属性内容。
public SettingItemView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
initView();
//获得自定义组件的属性值
attrsTitle = attrs.getAttributeValue(NAMESPACE, "title");
attrsDesc_ON = attrs.getAttributeValue(NAMESPACE, "desc_on");
attrsDesc_OFF = attrs.getAttributeValue(NAMESPACE, "desc_off");
setTitle(attrsTitle);//创建时,第一次加载“自动定更新设置”
setDsc(attrsDesc_OFF);//创建时,第一次加载“更新提示已关闭”
}