对一些常用的几个控件进行组合,封装成一个大的控件。
比如底部导航上的刷新和返回按钮,多处用到,此时则封装成大的控件,事件处理可以在大控件内,也可以在外面。
说白了:布局。只是布局出现的地方不一样,一个是代码中,一个是xml中。
废话少说,代码:
public class BackControl extends LinearLayout{
private Context context;
private Button backBtn;
public Button flashBtn;
private View view;
public BackControl(Context context) {
super(context);
this.context = context;
}
public BackControl(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
/**
* 绑定布局,
*/
public void bindLinearLayout(final int subModel) {
LayoutInflater inflater = LayoutInflater.from(context);
view = inflater.inflate(R.layout.user_bottom, null);
addView(view, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
backBtn = (Button) view.findViewById(R.id.user_back);
flashBtn = (Button) view.findViewById(R.id.user_flash);
//返回按钮
backBtn.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
Tool.forwardTarget((Activity)context, subModel);
}
});
}
如何使用:
back = (BackControl) findViewById(R.id.bulletlist_back);
back.bindLinearLayout(DesktopUI.KJ);
在xml中:
<com.acp.control.BackControl
android:layout_width="fill_parent" android:layout_weight="0"
android:layout_height="wrap_content" android:id="@+id/userbetting_back">
</com.acp.control.BackControl>
</LinearLayout>