日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android之自定义属性

發布時間:2024/7/23 Android 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android之自定义属性 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

安卓自定義屬性主要有3個步驟

  • 在values文件夾新建attrs.xml文件中聲明屬性,包括屬性名和格式,format常用屬性有string ,integer,reference等
  • <?xml version="1.0" encoding="utf-8"?> <resources><!-- 聲明屬性集的名稱 --><declare-styleable name="MyToggleButtton"><!-- 聲明屬性的name與類型 --><attr name="my_background" format="reference"/><attr name="my_slide_btn" format="reference"/><attr name="curr_state" format="boolean"/></declare-styleable></resources>
  • 在布局文件中使用,使用之前必須先聲明命名空間,前面是固定不變的內容,后面是包名.
  • <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:zj="http://schemas.android.com/apk/res/com.zj.switchbutton"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="${relativePackage}.${activityClass}" ><com.zj.switchbutton.MyTrouggleButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"zj:my_background="@drawable/switch_background"zj:my_slide_btn="@drawable/slide_button"zj:curr_state="true"/></RelativeLayout>
  • 在自定義view的構造方法中,通過解析AttributeSet方法,獲得所需要的屬性值,解析AttributeSet主要有兩種方法
  • 第一種:通過attrs.getAttributeValue獲得

    int counts=attrs.getAttributeCount();for(int i=0;i<counts;i++){attrs.getAttributeName(i);attrs.getAttributeValue(i);}public SettingItemView(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stubiniView(context);String title = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.zj.mobilesafe", "mytitle");desc_on = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.zj.mobilesafe", "desc_on");desc_off = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.zj.mobilesafe", "desc_off");tv_title.setText(title);setDesc(desc_off);}

    第二種:通過TypedArray獲得

    public MyTrouggleButton(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stub//獲得自定義屬性TypedArray ta=context.obtainStyledAttributes(attrs,R.styleable.MyToggleButtton);int N=ta.getIndexCount();for(int i=0;i<N;i++){int itemId=ta.getIndex(i);switch (itemId) {case R.styleable.MyToggleButtton_curr_state:current_state=ta.getBoolean(itemId, false);break;case R.styleable.MyToggleButtton_my_background:backgroundID=ta.getResourceId(itemId, -1);if(backgroundID==-1){throw new RuntimeException("請設置背景圖片");}backgroundBitmap=BitmapFactory.decodeResource(getResources(),backgroundID);break;case R.styleable.MyToggleButtton_my_slide_btn:slideButtonID=ta.getResourceId(itemId, -1);if(backgroundID==-1){throw new RuntimeException("請設置圖片");}slideBtnBitmap=BitmapFactory.decodeResource(getResources(), slideButtonID);default:break;}}init();}

    自定義屬性到底有什么用呢?當界面上的自定義元素有一些值需要改變并且大量重復的時候,自定義屬性可以有效的提高代碼的重用性,下面是一個簡單的例子

    聲明屬性

    <?xml version="1.0" encoding="utf-8"?> <resources><declare-styleable name="TextView"><attr name="mytitle" format="string" /><attr name="desc_on" format="string" /><attr name="desc_off" format="string" /></declare-styleable> </resources>

    在xml文件中定義

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:zj="http://schemas.android.com/apk/res/com.zj.mobilesafe"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TextViewandroid:id="@+id/textView1"android:layout_width="fill_parent"android:layout_height="55dip"android:background="#8866ff00"android:gravity="center"android:text="設置中心"android:textColor="#000000"android:textSize="22sp" /><com.zj.mobilesafe.ui.SettingItemViewandroid:id="@+id/siv_update"android:layout_width="wrap_content"android:layout_height="65dip"zj:desc_off="設置自動更新已經關閉"zj:desc_on="設置自動更新已經開啟"zj:mytitle="設置自動更新" ></com.zj.mobilesafe.ui.SettingItemView><com.zj.mobilesafe.ui.SettingItemViewandroid:id="@+id/siv_show_address"android:layout_width="wrap_content"android:layout_height="65dip"zj:desc_off="設置顯示號碼歸屬地已經關閉"zj:desc_on="設置顯示號碼歸屬地已經開啟"zj:mytitle="設置顯示號碼歸屬地" ></com.zj.mobilesafe.ui.SettingItemView><com.zj.mobilesafe.ui.SettingClickViewandroid:id="@+id/scv_changebg"android:layout_width="wrap_content"android:layout_height="65dip"></com.zj.mobilesafe.ui.SettingClickView><com.zj.mobilesafe.ui.SettingItemViewandroid:id="@+id/siv_callsms_safe"android:layout_width="wrap_content"android:layout_height="wrap_content"zj:desc_off="黑名單攔截已經關閉"zj:desc_on="黑名單攔截已經開啟"zj:mytitle="黑名單攔截設置" ></com.zj.mobilesafe.ui.SettingItemView><com.zj.mobilesafe.ui.SettingItemViewandroid:id="@+id/siv_watchdog"android:layout_width="wrap_content"android:layout_height="wrap_content"zj:desc_off="看門狗已經關閉"zj:desc_on="看門狗已經開啟"zj:mytitle="程序鎖設置" ></com.zj.mobilesafe.ui.SettingItemView></LinearLayout>

    解析屬性并且改變屬性

    /*** 自定義的組合控件* @author Administrator**/ public class SettingItemView extends RelativeLayout {private CheckBox cb_status;private TextView tv_desc;private TextView tv_title;private String desc_on;private String desc_off;/*** 初始化布局文件* @param context*/private void iniView(Context context) {// TODO Auto-generated method stubView.inflate(context, R.layout.setting_item_view, SettingItemView.this);cb_status=(CheckBox) this.findViewById(R.id.cb_status);tv_desc=(TextView) this.findViewById(R.id.tv_desc);tv_title=(TextView) this.findViewById(R.id.tv_title);}public SettingItemView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);// TODO Auto-generated constructor stubiniView(context);}/*** 帶有兩個參數的構造方法,布局文件使用的時候調用 * @param context* @param attrs*/public SettingItemView(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stubiniView(context);String title = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.zj.mobilesafe", "mytitle");desc_on = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.zj.mobilesafe", "desc_on");desc_off = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.zj.mobilesafe", "desc_off");tv_title.setText(title);setDesc(desc_off);}public SettingItemView(Context context) {super(context);// TODO Auto-generated constructor stubiniView(context);}/*** * 檢驗組合和控件是否有焦點*/public boolean isChecked(){return cb_status.isChecked();}/*** 設置組合控件的是否選中*/public void setChecked(boolean checked){if(checked){setDesc(desc_on);}else{setDesc(desc_off);}cb_status.setChecked(checked);}/*** 組合控件 的內容發生改變* */public void setDesc(String text){tv_desc.setText(text);}}

    效果如下

    總結

    以上是生活随笔為你收集整理的Android之自定义属性的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。