有時(shí)候一個(gè)布局需要重復(fù)被使用,為了避免更改其中一個(gè)布局可能造成所有相同的布局都要更改的局面,所以把這些相同的布局抽取出來,封裝成一個(gè)布局文件顽染。
本次組合是使用一個(gè)TextView加上一個(gè)CheckBox,大概的樣子就如圖所示了轰绵。
創(chuàng)建一個(gè)setting_custom.xml的文件粉寞,布局如下。
取消掉CheckBox的點(diǎn)擊事件左腔,使得整個(gè)控件可以被點(diǎn)擊唧垦。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#33000000">
<TextView
android:id="@+id/tv_custom_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="這是一個(gè)示例"
android:textSize="20sp"/>
<!--取消掉CheckBox的點(diǎn)擊事件-->
<CheckBox
android:id="@+id/cb_custom_check"
android:clickable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_margin="5dp"/>
</RelativeLayout>
在CustomSetting.java中開始寫代碼
package com.example.customwidget;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
/*
創(chuàng)建一個(gè)CustomSetting去繼承LinearLayout,然后寫出CustomSetting的構(gòu)造函數(shù)
*/
public class CustomSetting extends LinearLayout {
private View item;
private TextView tv_con;
private CheckBox cb_check;
/*
在構(gòu)造函數(shù)中進(jìn)行初始化界面和事件
*/
public CustomSetting(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
initEvent();
}
private void initEvent() {
cb_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
tv_con.setText("被勾選了");
}else {
tv_con.setText("沒被勾選");
}
}
});
}
private void initView() {
item = View.inflate(getContext(), R.layout.setting_custom, null);
addView(item);
tv_con = (TextView) item.findViewById(R.id.tv_custom_content);
cb_check = (CheckBox) item.findViewById(R.id.cb_custom_check);
}
/*
以下是對(duì)外界提供一些方法液样,可以用來更改組合控件的文字顯示振亮,以及使用點(diǎn)擊事件
*/
public void setCustomContent(String text){
tv_con.setText(text);
}
public void setIsChecked(boolean isChecked){
cb_check.setChecked(isChecked);
}
public boolean isChecked(){
return cb_check.isChecked();
}
public void setItemClickListener(OnClickListener listener){
item.setOnClickListener(listener);
}
}
在activity_main.xml中設(shè)置activity的主界面
<?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="match_parent">
<!--使用包名+類名-->
<com.example.customwidget.CustomSetting
android:id="@+id/cs_main"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.example.customwidget.CustomSetting>
</RelativeLayout>
package com.example.customwidget;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
private CustomSetting cs_con;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();
initEvent();
}
private void initEvent() {
cs_con.setCustomContent("暫時(shí)還沒有被點(diǎn)擊"); //設(shè)置文字
//對(duì)整個(gè)控件使用點(diǎn)擊事件,從而改變CheckBox的狀態(tài)
cs_con.setItemClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cs_con.setIsChecked(!cs_con.isChecked());
}
});
}
private void initView() {
setContentView(R.layout.activity_main);
//需要向下轉(zhuǎn)型成CustomSetting
cs_con = (CustomSetting) findViewById(R.id.cs_main);
}
}
最終效果如下
點(diǎn)擊后:
取消勾選: