【问题标题】:Logic for include layout [closed]包含布局的逻辑[关闭]
【发布时间】:2020-04-29 16:22:24
【问题描述】:

我想在 6 种不同的布局中包含相同的布局。布局有 2 个按钮。我需要对用于所有布局的按钮进行相同的操作。如何编写一次按钮逻辑并在其他 6 种布局中使用?

【问题讨论】:

  • 您可以使用数据绑定将逻辑添加到您的布局中,或者在某个地方编写一个方法,在其中编写逻辑并在需要的地方调用该方法

标签: android android-layout view


【解决方案1】:

创建自定义布局

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.....

【讨论】:

    猜你喜欢
    • 2012-06-30
    • 2012-08-20
    • 2010-09-11
    • 1970-01-01
    • 2019-11-25
    • 1970-01-01
    • 1970-01-01
    • 2013-03-15
    • 1970-01-01
    相关资源
    最近更新 更多