安卓:当设置编程layout_weight不工作 [英] Android: layout_weight not working when it is set programmatically

查看:153
本文介绍了安卓:当设置编程layout_weight不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,当我设置的TableRow layout_weight 编程。
继code是 TableLayout 虚增在 table_body 元素:

I have a problem when I set the TableRow layout_weight programmatically. Following code is the TableLayout inflated in table_body element:

<TableLayout
     android:layout_width="match_parent"
     android:layout_height="0px"
     android:layout_weight="1"
     android:weightSum="1">
<TableLayout>

我想在 TableLayout ..只显示10行,要做到这一点我添加的TableRow 元素的编程使用,这是code:

I want to display only 10 rows in TableLayout.. and to do this i add TableRow elements programmatically using this is the code:

while(i<10){
    TableRow row = (TableRow) ((Activity) context).getLayoutInflater().
        inflate(R.layout.body_row, table_body, false);

    TableRow.LayoutParams row_params = 
        new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, 0, 0.1f);

    table_body.addView(row, row_params);

    i++;
}

body_row.xml有这个code:

body_row.xml has this code:

<TableRow>
    <TextView 
        android:text="row1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</TableRow>

有了这个code TableLayout 不等于10除以空间(因为我愿意),但行只与间的空白,结果包裹的内容最后一排和TableLayout的结束而结束。什么我做错了吗?
谢谢!!

With this code TableLayout is not divided in 10 equal space (as I'd want) but rows only wrap their content with the result of blank space between the end of last row and the end of the TableLayout.. What I'm doing wrong? Thank you!!

推荐答案

为了让你需要使用适当的重量机制工作的LayoutParams 为您的膨胀的TableRow 小部件。由于的TableRow 的母公司是 TableLayout ,那么你需要使用 TableLayout.LayoutParams

To make the weight mechanism work you need to use the proper LayoutParams for your inflated TableRow widgets. As the parent of TableRow is a TableLayout then you need to use the TableLayout.LayoutParams:

TableLayout.LayoutParams row_params = new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, 0.1f);
table_body.addView(row, row_params);

这篇关于安卓:当设置编程layout_weight不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆