我想在6种不同的布局中包含相同的布局。该布局有2个按钮。对于用于所有布局的按钮,我需要执行相同的操作。如何编写一次按钮逻辑并在其他6种布局中使用它?
创建自定义布局
public class ButtonLayout extends RelativeLayout {
private Context context;
public ButtonLayout (Context context) {
super(context);
init();
}
public ButtonLayout (Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
init();
}
public ButtonLayout (Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
init();
}
private void init (){
inflate(context, R.layout.layout_buttons,this);
Button button1 = findViewById(R.id.button1); //and handle button logic
}}
然后将类添加到您的布局中
<com.stackoverflow.custom butt.....
只需在其中一个类中创建一个公共void(),然后在其中编写代码。然后,您只需要在点击监听器中调用它即可。