Android:RelativeLayout.LEFT_OF不起作用?

时间:2012-07-18 21:09:42

标签: android relativelayout

另一个我无法回答的问题: 我有一个RelativeLayout,它的右边应该有一个按钮,左边有一个ImageButton。我不知道如何安排这个。

我尝试的是:

    RelativeLayout TopLayout = (RelativeLayout) findViewById(R.id.topLayout);
    TopLayout.removeAllViews();
    TopLayout.setPadding(m_TableRowPadding_px, 8, m_TableRowPadding_px, 4);

    RelativeLayout.LayoutParams bParams = new RelativeLayout.LayoutParams(m_Resources
        .getDimensionPixelSize(R.dimen.ButtonWidth), m_DefaultButtonHeight_px);
    bParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    bParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);

    Button itemAddButton = new Button(this);
    itemAddButton.setLayoutParams(bParams);
    itemAddButton.setText(m_Resources.getString(R.string.add_item_button));
    itemAddButton.setOnClickListener(new View.OnClickListener()
    {...});

    TopLayout.addView(itemAddButton);

    RelativeLayout.LayoutParams ibParams = new RelativeLayout.LayoutParams(MIN_IMG_BUTTON_WIDTH,
        m_DefaultButtonHeight_px);
    ibParams.addRule(RelativeLayout.LEFT_OF, itemAddButton.getId());

    ImageButton speechButton = new ImageButton(this);

    speechButton.setLayoutParams(ibParams);
    speechButton.setContentDescription(m_Resources.getString(R.string.AddSpeechItemString));
    speechButton.setOnClickListener(new View.OnClickListener()
    {... });
    speechButton.setImageDrawable(m_Resources.getDrawable(R.drawable.micro2));

    TopLayout.addView(speechButton);

  }

RelativeLayout TopLayout = (RelativeLayout) findViewById(R.id.topLayout); TopLayout.removeAllViews(); TopLayout.setPadding(m_TableRowPadding_px, 8, m_TableRowPadding_px, 4); RelativeLayout.LayoutParams bParams = new RelativeLayout.LayoutParams(m_Resources .getDimensionPixelSize(R.dimen.ButtonWidth), m_DefaultButtonHeight_px); bParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); bParams.addRule(RelativeLayout.ALIGN_PARENT_TOP); Button itemAddButton = new Button(this); itemAddButton.setLayoutParams(bParams); itemAddButton.setText(m_Resources.getString(R.string.add_item_button)); itemAddButton.setOnClickListener(new View.OnClickListener() {...}); TopLayout.addView(itemAddButton); RelativeLayout.LayoutParams ibParams = new RelativeLayout.LayoutParams(MIN_IMG_BUTTON_WIDTH, m_DefaultButtonHeight_px); ibParams.addRule(RelativeLayout.LEFT_OF, itemAddButton.getId()); ImageButton speechButton = new ImageButton(this); speechButton.setLayoutParams(ibParams); speechButton.setContentDescription(m_Resources.getString(R.string.AddSpeechItemString)); speechButton.setOnClickListener(new View.OnClickListener() {... }); speechButton.setImageDrawable(m_Resources.getDrawable(R.drawable.micro2)); TopLayout.addView(speechButton); }

但结果是右边的按钮(根据需要)和左边的ImageButton。 :(

有人能帮帮我吗? O.o

干杯

克里斯

2 个答案:

答案 0 :(得分:4)

在使用该行之前,您需要为itemAddButton按钮设置一个id。现在itemAddButton.getId()只返回-1。

ibParams.addRule(RelativeLayout.LEFT_OF, itemAddButton.getId());

您可以设置类似

的内容
itemAddButton.setId(5005000);

答案 1 :(得分:0)

尝试向speechButton添加其他规则:

ibParams.addRule(RelativeLayout.ALIGN_TOP, itemAddButton.getId());

如果效果更好,您还可以使用RelativeLayout.ALIGN_BOTTOM