【问题标题】:Tablerow height doesn't changed?餐桌高度没有改变?
【发布时间】:2012-11-06 20:05:37
【问题描述】:

例如,我以编程方式创建 4 个表格行,并且我希望每个表格行的高度为屏幕大小的 1/4。每个 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>

【问题讨论】:

    标签: android android-4.0-ice-cream-sandwich tablerow android-tablelayout layoutparams


    【解决方案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));
    

    【讨论】:

    • 没用。表格的高度仍然是屏幕大小的一半。如您所见,我也尝试将高度设置为 10,但它没有改变。
    【解决方案2】:

    您没有使用正确的LayoutParams,对于 TableLayoutyou 应该设置LinearLayout.LayoutParams(当您将表添加到LinearLayout)和您的@987654325 @LayoutParams 应该是TableLayout.LayoutParams(因为它们是TableLayout 的孩子)。即使进行了上述更改,事情也不会起作用,因为TableLayout 是一个对其子级施加约束的小部件,最值得注意的是,对于TableRows,高度将自动设置为WRAP_CONTENT。改变这一点的唯一方法是像这样使用weight

    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 设置为填充父级,父级是ScrollView,不会对其子级施加尺寸。我不知道你想用那个ScrollView 做什么(特别是当你希望表格填满整个屏幕时)所以我不知道该推荐你什么。另外,我不知道您的完整布局,但您嵌套到许多布局。

    【讨论】:

    • 非常感谢,它有效。我不需要 Scrollview 我只是在测试中添加了它,当我看不到屏幕上的所有表格时。
    猜你喜欢
    • 1970-01-01
    • 2011-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-15
    相关资源
    最近更新 更多