【发布时间】:2020-04-29 16:22:24
【问题描述】:
我想在 6 种不同的布局中包含相同的布局。布局有 2 个按钮。我需要对用于所有布局的按钮进行相同的操作。如何编写一次按钮逻辑并在其他 6 种布局中使用?
【问题讨论】:
-
您可以使用数据绑定将逻辑添加到您的布局中,或者在某个地方编写一个方法,在其中编写逻辑并在需要的地方调用该方法
标签: android android-layout view
我想在 6 种不同的布局中包含相同的布局。布局有 2 个按钮。我需要对用于所有布局的按钮进行相同的操作。如何编写一次按钮逻辑并在其他 6 种布局中使用?
【问题讨论】:
标签: android android-layout view
创建自定义布局
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.....
【讨论】: