Custom ItemToggleView

Highly recommended article: Welcome Favorite
Android Dry Share

Reading five minutes, ten o'clock daily, lifelong learning with you, here is the Android programmer

This article describes the Androidpart of the development of knowledge by reading this article, you will reap the following:

  1. Custom View class that implements
  2. Custom View tab
  3. Custom Layout View
  4. Custom View Selector
  5. Custom View clip
  6. Activity custom view layout reference
  7. Activity uses a custom View

Custom ItemToggleViewcommonly used Settingsin opening and closing the main control switch.

Custom ItemToggleViewachieve results as follows:

Open .jpg
Close .jpg

1. Custom View class implemented

public class ItemToggleView extends RelativeLayout {
    private static final String TAG = "ItemToggleView";
    private TextView tv_title;
    private TextView tv_des;
    private static final String NAMESPACE = "http://schemas.android.com/apk/res/com.programandroid";
    private String mDesTitle;
    private String mDesOff;
    private String mDesOn;
    private ImageView mImageView;
    private boolean isOnOFF;

    public ItemToggleView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initAttrs(attrs);
        initUI(context);
    }

    public ItemToggleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttrs(attrs);
        initUI(context);
    }

    public ItemToggleView(Context context) {
        super(context);
        initUI(context);
    }

    // 单独抽取出来的 xml--->view
    private void initUI(Context context) {
        View.inflate(context, R.layout.item_toggle_view, this);

        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_des = (TextView) findViewById(R.id.tv_des);

        mImageView = (ImageView) findViewById(R.id.switch_imageview);

        tv_title.setText(mDesTitle);
    }

    public boolean getCheck() {
        return isOnOFF;
    }

    /**
     * @param isCheck
     *            传递一个选中未选中的状态变量(true 选中 false未选中)
     */
    public void setCheck(boolean isCheck) {
        // mSwitchControlView.setChecked(isCheck);
        if (isCheck) {
            tv_des.setText(mDesOn);
            mImageView.setImageDrawable(getResources().getDrawable(
                    R.drawable.toggle_on));

        } else {
            tv_des.setText(mDesOff);
            mImageView.setImageDrawable(getResources().getDrawable(
                    R.drawable.toggle_off));
        }
        isOnOFF = isCheck;
    }

    /**
     * @param attrs
     *            包含了属性名称和属性值的set集合
     */
    private void initAttrs(AttributeSet attrs) {
        // 打印属性总个数
        /*
         * Log.i(tag, "attrs.getAttributeCount() = "+attrs.getAttributeCount());
         * for(int i=0;i<attrs.getAttributeCount();i++){ //获取所有的属性名称 Log.i(tag,
         * "属性名称 = "+attrs.getAttributeName(i)); Log.i(tag,
         * "属性值 = "+attrs.getAttributeValue(i)); }
         */

        mDesTitle = attrs.getAttributeValue(NAMESPACE, "desTitle");
        mDesOff = attrs.getAttributeValue(NAMESPACE, "desOff");
        mDesOn = attrs.getAttributeValue(NAMESPACE, "desOn");

        Log.i(TAG, mDesTitle);
        Log.i(TAG, mDesOff);
        Log.i(TAG, mDesOn);
    }
}

2. Custom View tab

1. Note: Custom Android namespace

The same Androidnamespace (xmlns:android="http://schemas.android.com/apk/res/android" )method as want to use custom viewattributes, you must declare a custom viewnamespace
(xmlns:programandroid="http://schemas.android.com/apk/res/com.programandroid")

2. Note: Custom View Properties

Custom Viewproperties are as follows:

  programandroid:desOff="  不选中"
  programandroid:desOn="  选中"
  programandroid:desTitle=" WIFI " 

Properties declared in res/values/attrs.xmldefinition

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="ItemCheckView"><!--SettingItemView申明控件,内部可以包含自定义属性-->
        <attr name="desTitle" format="string"/><!--标题描述属性-->
        <attr name="desOff" format="string"/><!--关闭更新属性-->
        <attr name="desOn" format="string"/><!--开启更新属性-->
    </declare-styleable>
</resources>

3. Customize View Layout

<?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:layout_margin="5dp" >

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/primary_text_light"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/tv_des"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_title"
        android:textColor="@android:color/secondary_text_light"
        android:textSize="14sp" />

    <ImageView
        android:id="@+id/switch_imageview"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@drawable/toggle_on" />

</RelativeLayout>

4. Custom View Selector

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

    <!-- 按下去的背景颜色显示效果 -->
    <item android:drawable="@drawable/list_item_bg_light_pressed" android:state_pressed="true"/>
    <!-- 获取焦点时背景颜色显示效果 -->
    <item android:drawable="@drawable/list_item_bg_light_pressed" android:state_focused="true"/>
    <!-- 没有任何状态下的背景颜色 -->
    <item android:drawable="@drawable/list_item_bg_light_normal"/>

</selector>

5. Custom View creative

toggle_off.png
toggle_on.png

6. Activity custom view layout reference

    <com.programandroid.CustomView.ItemToggleView
        xmlns:programandroid="http://schemas.android.com/apk/res/com.programandroid"
        android:id="@+id/custom_item_toggle_view"
        android:layout_width="wrap_content"
        android:layout_height="55dp"
        android:background="@drawable/listview_item_selector"
        programandroid:desOff="  关闭"
        programandroid:desOn="  开启"
        programandroid:desTitle=" WIFI " />

7. Activity Use Custom View

    /**
     * 自定义 ItemToggleView
     */
    private void InitItemToggleView() {
        // TODO Auto-generated method stub
        final ItemToggleView mItemToggleView = (ItemToggleView) findViewById(R.id.custom_item_toggle_view);
        mItemToggleView.setCheck(false);
        mItemToggleView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mItemToggleView.setCheck(!mItemToggleView.getCheck());
            }
        });
    }

So far herein, this has ended, if the wrong place, welcome your suggestions and corrections. At the same time look forward to your attention, thank you for reading, thank you!

Micro-channel public concern number: Programmer Android, receive welfare

Guess you like

Origin www.cnblogs.com/wangjie1990/p/11310695.html