2013-05-20 4 views
1

나는 GridLayout을 가지고 있고 두 개 이상의 버튼이 안에 있으며 모든 버튼은 같은 너비를 가져야합니다. 내 문제는 다른 줄 번호의 단추가있을 때 단추의 marginTop도 다를 것입니다. 이 문제를 어떻게 해결할 수 있습니까? 나는 모든 버튼을 같은 높이의 위치에두기를 원한다.버튼 layout_gravity = "fill_vertical"을 프로그래밍 방식으로 설정하는 방법은 무엇입니까?

감사합니다.

이미 GravityFILL_VERTICAL으로 설정하려고했지만 작동하지 않습니다.

My picture

이 지금 너무 노력하고 있습니다 : 여기

GridLayout grid_neighbor = (GridLayout) findViewById(R.id.grid_neighbors); 
grid_neighbor.setColumnCount(4); 
for (i = 0; i < namelist.length; ++i) { 
    Button temp = new Button(this); 
    temp.setText(namelist[i]);     

    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
    lp.gravity = Gravity.FILL_VERTICAL; 
    temp.setLayoutParams(lp); 
    //temp.setGravity(Gravity.FILL_VERTICAL); 
    temp.setWidth(button_size); 
    temp.setHeight(button_size); 
    grid_neighbor.addView(temp); 
} 

는 사진입니다.

GridLayout grid_neighbor = (GridLayout) findViewById(R.id.grid_neighbors); 
grid_neighbor.setColumnCount(4); 
for (i = 0; i < namelist.length; ++i) { 
    Button temp = new Button(this); 
    temp.setText(namelist[i]);     

    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
    lp.addRule(RelativeLayout.ALIGN_TOP, temp.getId()); 
    temp.setLayoutParams(lp); 
    //temp.setGravity(Gravity.FILL_VERTICAL); 
    temp.setWidth(button_size); 
    temp.setHeight(button_size); 
    grid_neighbor.addView(temp); 
} 

감사.

GridLayout.Spec speccolumn = GridLayout.spec(i%4); 
GridLayout.Spec specrow = GridLayout.spec(i/4, GridLayout.TOP); 
grid_neighbor.addView(temp, new GridLayout.LayoutParams(specrow, speccolumn)); 
+0

추가 규칙 첫 번째 버튼으로 상단 맞춤 – OMAK

+0

채우기 부모가 상위의 높이를 갖도록 버튼을 설정할 수 없습니까? – Hardik4560

+0

@OMAK addRule (RelativeLayout.ALIGN_TOP) 및 ALIGN_PARENT_TOP 모두 작동하지 않습니다. – JamesChiang

답변

0

GridLayout 행의 기준선을 설정해도 문제가 해결되지 않습니까? 그리드 요소에 텍스트 줄 수가 다른 경우 android는 행 요소를 정렬하지 않고 텍스트를 동일한 기준선에 자동 정렬하려고합니다.

편집 : rowSpec.alignment = TOP (또는 http://developer.android.com/reference/android/widget/GridLayout.Alignment.html에서 사용할 수있는 정렬 중 하나)를 설정하는 것이 더 적절하다고 생각합니다. 자세한 내용은 http://developer.android.com/reference/android/widget/GridLayout.LayoutParams.html을 참조하십시오.