2012-10-26 3 views
5

버튼 사이에 20px의 하단 여백을 사용하여 수직으로 버튼을 자동 생성하려고합니다. 나는 성공없이 LayoutParams 객체로 여백을 설정하려고합니다.android : LayoutParams가있는 버튼 사이의 여백 설정

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/regions_search" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_marginBottom="30dip" 
    android:orientation="vertical" > 
</LinearLayout> 



@Override 
public void onCreate(Bundle savedInstanceState) { 

    ... 

    for (Region region : regionsList) { 

     //create new button  
     Button button = new Button(mContext); 

     //set infos 
     int id = Integer.parseInt(Long.toString((Long) region.getId()));  button.setId(id); 
     button.setText(region.getName() + "(" + region.getStores_nb() + ")"); 

     //Layoutparams setting 
     FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 
     params.setMargins(0, 0, 0, 20); 

     button.setLayoutParams(params); 

     myLinear.addView(button); 

     } 

이미지에서 볼 수 있듯이 이미지 사이에는 공백이 없습니다. 왜 그 사람이 누군지 알아? 감사합니다. enter image description here

+0

가 왜 FrameLayout이 사용합니까? XML 파일은 LinearLayout에 있습니다. –

+0

완료, 고맙습니다. – johann

답변

9

이 작업을 시도 할 수 있습니다 :

LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) button.getLayoutParams(); 
layoutParams.bottomMargin += 20; 
button.setLayoutParams(layoutParams); 
+0

고마워! 여러분 모두 나에게 똑같은 대답을 주지만, 여러분은 처음으로 게시했습니다! 고마워요 ~ – johann

+0

실제로 누누가 첫 번째 였다고 생각합니다.) ...하지만 감사합니다 – Cata

+0

이것 좀보세요 http://setackoverflow.com/questions/16552811/set-a-margin-between-two-buttons-programmatically-from- 선형 레이아웃? noredirect = 1 # comment23779639_16552811 – Dimitri

2

시도는 LinearLayout를 사용하여 XML과 같이 대신 FrameLayout.LayoutParamsLinearLayout.LayoutParams를 사용하는 ..

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 
+1

카타는 당신보다 빠릅니다. 미안합니다. 도와 줘서 고마워. – johann