2012-07-18 63 views
0

另一個問題,我找不到答案: 我有一個RelativeLayout,它應該有一個按鈕在其右邊緣和一個ImageButton靠近左邊。我不知道如何安排這一點。Android:RelativeLayout.LEFT_OF不起作用?

我嘗試是:

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到左側的按鈕。 :(

誰能幫我?哦哦

乾杯

克里斯

回答

4

您需要u使用前行到一個ID設置爲itemAddButton按鈕。現在itemAddButton.getId()會剛剛返回-1。

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

您可以設置類似

itemAddButton.setId(5005000); 
+0

謝謝:) 從來沒有想過,該ID必須由程序員設置:( – 2012-07-19 06:39:24

0

嘗試增加額外的規則,你speechButton

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

您還可以使用RelativeLayout.ALIGN_BOTTOM是否適合你更好。