2012-06-14 32 views
0

我試圖根據屏幕寬度動態添加按鈕。根據屏幕寬度動態添加按鈕

即,如果我得到6個按鈕,那麼我需要相應地定位它們,以便按鈕出現在中間,在左父母和右父母上具有相等的間距。

這裏是一段代碼,其我想,但沒有結果:

private void btmBarBtns(int position) { 

    RelativeLayout rlLayout; 
    RelativeLayout.LayoutParams layoutParams; 

    int leftMargin = scrWidth/pageCount; 

    CommonMethods.getSystemOutput("Left Margin::::"+leftMargin); 

    for (int i = 0; i < pageCount; i ++) { 

     rlLayout = (RelativeLayout) findViewById(R.id.ivBottomBar); 

     layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 

     layoutParams.leftMargin = leftMargin; 

     ib = new ImageButton(this); 
     ib.setId(i); 
     ib.setLayoutParams(layoutParams); 
     ib.setBackgroundResource(R.drawable.white_circle_32x32); 

     rlLayout.addView(ib); 
     leftMargin = leftMargin + 70; 

     if (ib.getId() == position) { 
      ib.setBackgroundResource(R.drawable.black_circle_32x32); 
     } 

    } 
} 

在上面的代碼我有高度25dp和寬度FILL_PARENT的相對佈局。我可以添加按鈕,但它們不在中心。

回答

1

如果你想爲中心的ImageButtons等間隔左右,那麼你可以簡單的將它們包裝在一個LinearLayout,然後居中,在父RelativeLayoutLinearLayout

RelativeLayout rlLayout = (RelativeLayout) findViewById(R.id.parent); 
    LinearLayout container = new LinearLayout(this); 
    for (int i = 0; i < 5; i++) { 
     LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
       LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
     ImageButton ib = new ImageButton(this); 
     ib.setId(i); 
     ib.setLayoutParams(layoutParams); 
     ib.setBackgroundResource(R.drawable.ic_launcher); 
     container.addView(ib); 

     if (ib.getId() == position) { 
      ib.setBackgroundResource(R.drawable.black_circle_32x32); 
     } 
    } 
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
      LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
    layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL, 
      RelativeLayout.TRUE); 
    rlLayout.addView(container, layoutParams); 

如果你想寫更多的代碼只是做上述那麼你可以修改當前的佈局,並添加這個元素作爲錨:

<View 
    android:layout_width="0dp" 
    android:layout_height="0dp" 
    android:layout_centerHorizontal="true" 
    android:id="@+id/anchor" /> 

,然後在代碼中的位置ImageButtons這種定位的View的左,右:

int anchorId = R.id.anchor;  
     int btnsNr = 6; // this is the number of Buttons 
     RelativeLayout rlLayout = (RelativeLayout) findViewById(R.id.parent); 
     if (btnsNr % 2 != 0) { 
      anchorId = 1000; 
      btnsNr--; 
      ImageButton imgb = new ImageButton(this); 
      imgb.setImageResource(R.drawable.shop_open); 
      imgb.setId(anchorId); 
      RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT, 
        RelativeLayout.LayoutParams.WRAP_CONTENT); 
      rlp.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); 
      rlLayout.addView(imgb, rlp); 
     } 
     int whichPart = 1; 
     while (whichPart >= 0) { 
      int previousId = anchorId; 
      for (int i = 0; i < (btnsNr/2); i++) { 
       RelativeLayout.LayoutParams tmp = new RelativeLayout.LayoutParams(
         RelativeLayout.LayoutParams.WRAP_CONTENT, 
         RelativeLayout.LayoutParams.WRAP_CONTENT); 
       if (whichPart == 1) { 
        tmp.addRule(RelativeLayout.LEFT_OF, previousId); 
       } else { 
        tmp.addRule(RelativeLayout.RIGHT_OF, previousId); 
       } 
       ImageButton imgb = new ImageButton(this); 
       previousId += whichPart == 1 ? -1 : 1; 
       imgb.setId(previousId); 
       imgb.setImageResource(R.drawable.shop_open); 
       rlLayout.addView(imgb, tmp); 
      } 
      whichPart--; 
     } 

如果你想計算的ImageButtons適合屏幕的數量(和水平居中他們),你應該提到。

+0

我使用了一個相對佈局(從兒童到父母的相對佈局),包含高度和寬度的內容,將它對齊到中心位置並將按鈕添加到它。我認爲沒有必要添加視圖容器。我通過指定邊距動態地添加按鈕。它爲我工作。謝謝你的幫助!!! – krisDrOid

+0

@krisDrOid添加「視圖」是另一種方式(使用更多的代碼)。我使用了一個'LinearLayout',但當然你可以像你一樣使用'RelativeLayout'。 – Luksprog

相關問題