自定義控件layout.xml: java
<?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="70dip" android:orientation="vertical" > <TextView android:id="@+id/tv_settingview_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="3dip" android:layout_marginTop="3dip" android:text="標題" android:textColor="@color/black" android:textSize="28sp" /> <TextView android:id="@+id/tv_settingview_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv_settingview_title" android:layout_marginLeft="3dip" android:layout_marginTop="2dip" android:text="描述信息" android:textColor="#99000000" android:textSize="18sp" /> <CheckBox android:clickable="false" //不能被點擊 android:focusable="false" //不能獲取焦點 android:id="@+id/cb_settingview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" /> <View android:layout_alignParentBottom="true" android:layout_width="fill_parent" android:background="@drawable/list_devider" android:layout_height="1dip" > </View> </RelativeLayout>
自定義控件類: android
public class SettingView extends RelativeLayout { private View view; private TextView tv_setting_1; private TextView tv_setting_2; private CheckBox cb_setting; public SettingView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initView(context); } public SettingView(Context context, AttributeSet attrs) { super(context, attrs); initView(context); //把屬性集和咱們定義的屬性數組創建對應關係 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.my_setting_view); String content = a.getString(R.styleable.my_setting_view_content); String title = a.getString(R.styleable.my_setting_view_title); tv_setting_1.setText(title); tv_setting_2.setText(content); a.recycle(); } public SettingView(Context context) { super(context); initView(context); } private void initView(Context context){ //把xml文件轉化成對象,this:view對象以本身爲父體 view = View.inflate(context, R.layout.view_setting, this); tv_setting_1 = (TextView) view.findViewById(R.id.tv_setting_1); tv_setting_2 = (TextView) view.findViewById(R.id.tv_setting_2); cb_setting = (CheckBox) view.findViewById(R.id.cb_setting); } /** * 判斷是否被選中 * @return */ public boolean isCkecked(){ return cb_setting.isChecked(); } /** * 設置當前控件的選中狀態 * @param checked * @return */ public void setChecked(boolean checked){ cb_setting.setChecked(checked); } /** * 更改content裏面的內容 * */ public void setContent(String text, int color){ tv_setting_2.setText(text); tv_setting_2.setTextColor(color); } }
value目錄下的attrs.xml: 數組
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="my_setting_view"> <attr name="title" format="string" /> <attr name="content" format="string" /> </declare-styleable> </resources>
用到自定義控件的Activity的layout.xml中: app
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:itcast="http://schemas.android.com/apk/res/com.example.myapplication" //自定義控件的名稱空間 android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/rl_set" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@color/lightskyblue" android:orientation="horizontal" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="我的設置" android:textSize="25sp" /> </LinearLayout> <com.example.myapplication.SettingView itcast:title = "自動更新" itcast:content = "自動更新已經開啓" android:id="@+id/sv_setting" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
用到自定義控件的Activity:
public class SettingActivity extends Activity { private SettingView sv_setting; private SharedPreferences sp; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_setting); sp = getSharedPreferences("config", MODE_PRIVATE); sv_setting = (SettingView) findViewById(R.id.sv_setting); boolean update = sp.getBoolean("update", true); if(update){ sv_setting.setChecked(true); sv_setting.setContent("自動更新已經開啓", Color.BLACK); }else{ sv_setting.setChecked(false); sv_setting.setContent("自動更新沒有開啓", Color.RED); } sv_setting.setOnClickListener(new OnClickListener() { public void onClick(View v) { Editor editor = sp.edit(); if (sv_setting.isCkecked()) { sv_setting.setChecked(false); sv_setting.setContent("自動更新沒有開啓", Color.RED); editor.putBoolean("update", false); } else { sv_setting.setChecked(true); sv_setting.setContent("自動更新已經開啓", Color.BLACK); editor.putBoolean("update", true); } editor.commit(); } }); } }