爱玺玺

爱玺玺的生活日记本。wx:lb87626

理解自定义控件

自定义控件有个自己的样式文件 item_xx.xml文件

这里写好了自定义控件的样式,其实就是把基本样式重新组合成一个新的样式。


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:gravity="center_horizontal"

    android:orientation="vertical">

    <ImageView 

        android:id="@+id/ico"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:src="@drawable/safe"

        />

    <TextView 

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_below="@id/ico"

        android:textColor="#000000"

        android:id="@+id/title"

        android:layout_alignBaseline="@id/ico"

        android:text="手机防盗"/>

</LinearLayout>


然后还有个对应的java文件放在ui包里面

这里的关键是这个自定义的SeetingView类和样式文件setingview.xml文件到底是通过什么联系起来的。

这里是关键

// 自己增加的样式

private void init() {

// 第一种方式

// View view = View.inflate(getContext(), R.layout.settingview, null);

// this.addView(view);

// 第二种方式

View view = View.inflate(getContext(), R.layout.settingview, this);

title_tv = (TextView) view.findViewById(R.id.title);

des_tv = (TextView) view.findViewById(R.id.des);

cb = (CheckBox) view.findViewById(R.id.cb);

}


在每一个构造方法中都运行了这个方法,这个方法里面通过View.inflate调用了那个自定义的item样式文件。这个类继承了RelativeLayout,那么在activity的样式文件中使用这个类作为一个元素来使用。通过findViewbyId就能获取这个自定义对象。然后使用它的方法。


不要因为样式文件而混淆,要理解样式文件里面的元素最终实际上是转化成一个独立的对象。

    <cn.jianhaozhan.ui.SettingView

        android:id="@+id/sv_setting_update"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        jianhaozhan:st_title="提示更新"

        jianhaozhan:st_des_on="提示更新已开启"

        jianhaozhan:st_des_off="提示更新已关闭"

        ></cn.jianhaozhan.ui.SettingView>

这个就是自定义控件的在样式文件中的写法,这里最终是被自己的写的那个对象文件解析了。这里实际上是一个对象。这个对象通过修改命名空间还可以获取自己定义属性内容。

另外:

        jianhaozhan:st_title="提示更新"

        jianhaozhan:st_des_on="提示更新已开启"

        jianhaozhan:st_des_off="提示更新已关闭"

上面这几个属性不要和对象里面的属性混淆。对象里面其实本质还是TextView那些基本属性。对象里面其实就是通过attrs.getAttributeValue方法获取了里面的值再赋给item里面的TextView。


package cn.jianhaozhan.ui;


import cn.jianhaozhan.activity.R;

import android.R.integer;

import android.content.Context;

import android.util.AttributeSet;

import android.view.View;

import android.widget.CheckBox;

import android.widget.RelativeLayout;

import android.widget.TextView;


public class SettingView extends RelativeLayout {

private TextView title_tv;

private TextView des_tv;

private CheckBox cb;

private String st_title;

private String st_des_on;

private String st_des_off;


// 在布局文件中使用时调用,多了个样式

public SettingView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

init();

}


// 在布局文件中使用时调用,样式文件中设置属性

// AttributeSet里面有控件所有属性

public SettingView(Context context, AttributeSet attrs) {

super(context, attrs);

init();

st_title = attrs.getAttributeValue(

"http://schemas.android.com/apk/res/cn.jianhaozhan.activity",

"st_title");

st_des_on = attrs.getAttributeValue(

"http://schemas.android.com/apk/res/cn.jianhaozhan.activity",

"st_des_on");

st_des_off = attrs.getAttributeValue(

"http://schemas.android.com/apk/res/cn.jianhaozhan.activity",

"st_des_off");

//给自定义组合控件设置值,初始化控件的值

title_tv.setText(st_title);

if(isChecked()){

des_tv.setText(st_des_on);

}else{

des_tv.setText(st_des_off);

}

}


// 在代码中使用时调用

public SettingView(Context context) {

super(context);

init();

}


// 自己增加的样式

private void init() {

// 第一种方式

// View view = View.inflate(getContext(), R.layout.settingview, null);

// this.addView(view);

// 第二种方式

View view = View.inflate(getContext(), R.layout.settingview, this);

title_tv = (TextView) view.findViewById(R.id.title);

des_tv = (TextView) view.findViewById(R.id.des);

cb = (CheckBox) view.findViewById(R.id.cb);

}


/**

* 设置标题

* @param title

*/

public void setTitle(String title) {

title_tv.setText(title);

}


/**

* 设置内容

* @param des

*/

public void setDes(String des) {

des_tv.setText(des);

}


/**

* 设置选中状态

* @param isChecked

*/

public void setCheckBox(boolean isChecked) {

System.out.println("设置checkbox");

cb.setChecked(isChecked);

if(isChecked()){

des_tv.setText(st_des_on);

}else{

des_tv.setText(st_des_off);

}

}


/**

* 获取checkbox状态

* @return

*/

public boolean isChecked() {

return cb.isChecked();

}

}




然后在layout的其它activity样式文件和activity的java程序里面就可以使用这个自定义控件


在activity样式文件中使用自定义控件还需要加上自定义控件的命名空间

xmlns:jianhaozhan="http://schemas.android.com/apk/res/cn.jianhaozhan.activity"


还需要在values里面创建一个attrs.xml里面声明自定义控件的属性

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <declare-styleable name="cn.jianhaozhan.ui.SettingView">

        <attr name="st_title" format="string"/>

        <attr name="st_des_on" format="string"/>

        <attr name="st_des_off" format="string"/>

    </declare-styleable>

</resources>



发表评论:

Powered By Z-BlogPHP 1.4 Deeplue Build 150101

Copyright Your WebSite.Some Rights Reserved.

蜀ICP备11021721号-5