java - 检测布局中的第一个按钮

标签 java android android-layout android-studio android-relativelayout

我正在尝试解决这个难题:“如何让列表中的其余按钮位于第一个按钮下方。”

为了解决这个问题,我尝试设置一个 boolean 值来检测第一个按钮,并将规则应用于该按钮下面的任何内容,但button1似乎每次都在button0之上。

我在下面列出了 MainActivity。布局 xml 中只有一个相对布局。

public class MainActivity extends Activity implements View.OnClickListener {

private ArrayList<Category> categories = new ArrayList<Category>();
private ArrayList<Button> buttons = new ArrayList<Button>();
private boolean firstButton = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    RelativeLayout relLayout = (RelativeLayout) findViewById(R.id.mainLayout);

    categories.add(new Category("Mobility",CategoryTypes.MOBILITY));
    categories.add(new Category("Flexibility",CategoryTypes.FLEXIBILITY));
    categories.add(new Category("Mobility",CategoryTypes.MOBILITY));
    categories.add(new Category("Flexibility",CategoryTypes.FLEXIBILITY));
    categories.add(new Category("Mobility",CategoryTypes.MOBILITY));
    categories.add(new Category("Mobility",CategoryTypes.MOBILITY));

    for (int i = 0; i < 6; i++)
    {
        makeButton(this, "Buttons"+i,i);
    }

    for (int i = 0; i < buttons.size(); i++)
    {
        addButtonToLayout(buttons.get(i),relLayout,i);
        System.out.println(buttons.get(i).getLayoutParams());
    }
}

//This method constructs a button object, sets its text, id and an OnClickListener. Finally adds the buttons to buttons arraylist
void makeButton (Context context, String buttonText, int id)
{
    Button button = new Button(context);
    button.setText(buttonText);
    button.setId(id);
    button.setOnClickListener(this);

    buttons.add(button);
    System.out.println(button.getId());
}

//Method to add button to layout. The LayoutParams are used to add the rule that the buttons should be underneath each other.
void addButtonToLayout (Button button, RelativeLayout layout, int index)
{
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    RelativeLayout.LayoutParams paramsForSecondButton = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

    if (firstButton)
    {
        firstButton = false;
        System.out.println(buttons.get(index).getText());
        //TODO: [BUG] Doesn't add the first button for some reason.
    }
    else if (!firstButton)
    {
        params.addRule(RelativeLayout.BELOW, buttons.get(index-1).getId());
    }

    //paramsForSecondButton.addRule(RelativeLayout.BELOW, buttons.get(0).getId());

    //params.addRule(RelativeLayout.BELOW, buttons.get(index).getId());
    button.setLayoutParams(params);
   // buttons.get(1).setLayoutParams(paramsForSecondButton);

    layout.addView(button);
}

这是布局的图像: image of layout

最佳答案

有一个answer类似的问题应该对你有帮助。

The reason this is failing for you is because of the IDs you are using. Android uses "reserved" ids for things like the general content area of the app.

Using your code, I was able to add 1000 to each ID and generate the intended result.

以这种方式放置按钮也不是一个好主意。除非您有理由这样做,否则简单的 LinearLayout 将以一半的计算时间完成这项工作。

关于java - 检测布局中的第一个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30161661/

相关文章:

android - Ionic 2 RC0 和 Angular 2 最新构建 android 错误 (ngc : Error: Error encountered resolving symbol values statically)

android - 如何定义android中Horizo​​ntal recyclerview中的项目数量?

android - 将 View 宽度动态设置为父布局的百分比

java - Android - 如何在用户点击时为图像的整个区域着色并将其置于屏幕中央?

java - 千分尺在哪里存储数据?

java - 在没有文本框的情况下在内部从条形码扫描仪获取输入

java - Struts2 - 延迟 Action 执行

java - LibGdx Gdx-Pay 扩展获取商品价格

android - 多色边框EditText

java - mvc 和 GridBagLayout 中的 ActionPerformed 和 update(Model,Object)