android項目自訂群組合控制項添加屬性

來源:互聯網
上載者:User

標籤:

首先要在values檔案下建立立一個檔案arrts.xml,這個檔案就是用來說明鍵名稱是做什麼的,和值的類型

<?xml version="1.0" encoding="utf-8"?><resources>     <declare-styleable name="SeetingView">        <attr name="up" format="string" />        <attr name="down_on" format="string" />        <attr name="down_off" format="string" />    </declare-styleable></resources>

然後在應用的布局裡需要加上命名空間,第二行就是自己的命名空間,這個組成就是前面的xmlns:itheima名字然後是"http://schemas.android.com/apk/res/+包名

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   xmlns:itheima="http://schemas.android.com/apk/res/com.itheima.superman"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:background="@drawable/home_bg">    <TextView         style="@style/TitleStyle"        android:text="設定中心"        />    <com.itheima.view.SeetingView        android:id="@+id/stv_updata"        android:layout_marginTop="5dp"        android:layout_width="match_parent"        android:layout_height="wrap_content"        itheima:up="自動更新設定"        itheima:down_on="自動更新已經開啟"         itheima:down_off="自動更新已經關閉"        />   </LinearLayout>

最後我們就要在自訂控制項的類裡擷取這些值瞭然後給他們設定上。

package com.itheima.view;import com.itheima.superman.R;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 SeetingView extends RelativeLayout{    private TextView tv_up;    private TextView tv_down;    private CheckBox ck_right;    private String up;    private String down_on;    private String down_off;    //命名空間    private String nameSpace = "http://schemas.android.com/apk/res/com.itheima.superman";    //自訂樣式調用    public SeetingView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        initView();    }    //自訂屬性調用    public SeetingView(Context context, AttributeSet attrs) {        super(context, attrs);        //擷取命名空間下,鍵名稱為“up”的值        up = attrs.getAttributeValue(nameSpace, "up");        down_on = attrs.getAttributeValue(nameSpace, "down_on");        down_off = attrs.getAttributeValue(nameSpace, "down_off");        initView();    }    //聲明調用    public SeetingView(Context context) {        super(context);        initView();    }    //初始化布局    private void initView(){        //給這個布局一個父控制項        View.inflate(getContext(),R.layout.item_seeting, this);        tv_up = (TextView) findViewById(R.id.tv_up);        tv_down = (TextView) findViewById(R.id.tv_down);        ck_right = (CheckBox) findViewById(R.id.ck_right);        //在初始化的時候設定這個值        tv_up.setText(up);    }    //設定頂部文字    public void setUp(String text){        tv_up.setText(text);    }    //設定底部文字    public void setDown(String text){        tv_down.setText(text);    }    //是否被選中    public boolean isChecked(){        return ck_right.isChecked();    }    //設定選擇狀態    public void setChecked(boolean b){        if(b){            ck_right.setChecked(b);            tv_down.setText(down_on);        }else{            ck_right.setChecked(b);            tv_down.setText(down_off);        }    }    }

 

android項目自訂群組合控制項添加屬性

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.