包含布局的逻辑

问题描述 投票:-2回答:2

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

android android-layout view
2个回答
0
投票

创建自定义布局

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

-1
投票

只需在其中一个类中创建一个公共void(),然后在其中编写代码。然后,您只需要在点击监听器中调用它即可。

© www.soinside.com 2019 - 2024. All rights reserved.