2

예를 들어, 프로그래밍 방식으로 4 개의 tablerow를 만들고 각 tablerow 높이를 화면의 1/4 크기로 지정합니다. 각 tablerow 높이는 화면 크기의 절반입니다. 나는 다른 방법을 시도했지만, tablerow 높이는 변하지 않았다. 나는 tablerow 너비의 변화를 관리하지만 높이는 그렇지 않습니다. 여기 내 코드입니다 :Tablerow 높이가 변경되지 않았습니까?

@SuppressLint("NewApi") 
public class GameActivity extends Activity { 

    private Context context; 

    public void drawTable(){ 
     int i; 
     TableLayout table = new TableLayout(context); 

     table.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT)); 
     //table.setStretchAllColumns(true); 
     //table.setShrinkAllColumns(true); 
     //table.setWeightSum(1.0F); 
     TableRow[] rows = new TableRow[5]; 
     for(i = 0; i < 4; ++i){ 
      rows[i] = new TableRow(context); 
      rows[i].setBackgroundResource(R.drawable.images214); 
      rows[i].setWeightSum((float)1.0); 
      rows[i].setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 10)); 
      //rows[i].setScaleY(0.25F); 
      table.addView(rows[i]); 
     } 

     LinearLayout l = (LinearLayout) findViewById(R.id.linear); 
     l.addView(table); 
    } 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_game); 

     context = this; 
//  TableLayout table = new TableLayout(this); 
//  TableRow row1 = new TableRow(this); 
//  TableRow row2 = new TableRow(this); 
//  row1.addView(new EditText(this)); 
//  row2.addView(new Button(this)); 
//  table.addView(row1); 
//  table.addView(row2); 
//  //table.setBackground(getResources().getDrawable(R.drawable.ic_launcher)); 
//  table.setBackgroundResource(R.drawable.ic_launcher); 
//  LinearLayout l = (LinearLayout) findViewById(R.id.linear); 
//  l.addView(table); 
     drawTable(); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_game, menu); 
     return true; 
    } 
} 

XML : 각 테이블 행 설정 높이

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ScrollView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:scrollbars="none" 
    android:layout_weight="1"> 
     <LinearLayout 
     android:id="@+id/linear" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scrollbars="vertical|horizontal"> 
     </LinearLayout> 
</ScrollView> 
</RelativeLayout> 

답변

6

당신은 적절한 LayoutParams를 사용하지 않을의 테이블에 대한 TableLayoutLinearLayout.LayoutParams (테이블을 LinearLayout에 추가 할 때)과 TableRowsLayoutParamsTableLayout.LayoutParams이어야합니다 (어린이는 TableLayout이므로). 위의 변경 사항이 적용되는 경우에도 TableLayout은 자식에 제약 조건을 부과하는 위젯이므로 가장 중요한 것은 높이가 TableRows 인 경우 자동으로 WRAP_CONTENT으로 설정됩니다. 하지 자식에 치수를 부과 않는 ScrollView 인 부모,

TableLayout table = new TableLayout(context); 
table.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)); 
table.setWeightSum(1.0f); 
TableRow[] rows = new TableRow[4]; 
for(int i = 0; i < 4; ++i){ 
    rows[i] = new TableRow(context); 
    rows[i].setBackgroundResource(R.drawable.images214); 
    rows[i].setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, 0, 0.25f));   
    table.addView(rows[i]); 
} 

LinearLayout l = (LinearLayout) findViewById(R.id.linear); 
l.addView(table); 

을하지만 당신의 TableLayout가 부모를 채우기 위해 설정되어 있기 때문에이 작동하지 않습니다 :이 설정을 변경하는 유일한 방법은이 같은 weight를 사용하는 것입니다 . 나는 당신이 그걸로 무엇을하려고하는지 모르겠다. (특히 당신이 테이블을 전체 화면을 채우고 싶어하는 것처럼) 내가 무엇을 추천해야할지 모르겠다. 또한, 전체 레이아웃을 모르지만 많은 레이아웃을 중첩합니다.

+0

고맙습니다. 작동합니다. Scrollview가 필요하지 않습니다. 화면에서 모든 tablerows를 볼 수 없을 때 테스트 목적으로 추가했습니다. –

1

:

DisplayMetrics metrics = context.getResources().getDisplayMetrics(); 
int height = metrics.heightPixels/4; // quarter of screen height 
rows[i].setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, height)); 
+0

감사합니다. 시도해 보겠습니다. –

+0

도움이되지 않습니다. tablerow의 높이는 절반 화면 크기로 유지됩니다. 보시다시피 저는 높이 10을 설정하려고 시도했지만 변경되지 않았습니다. –

관련 문제