首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tablerow高度不变吗?

Tablerow高度不变吗?
EN

Stack Overflow用户
提问于 2012-11-19 05:17:19
回答 2查看 6.7K关注 0票数 2

例如,我通过编程创建了4个tablerow,我希望每个tablerow的高度是屏幕的1/4。每张桌子的高度是屏幕尺寸的一半。我尝试了不同的方法,但是桌面高度没有改变。我设法改变了tablerow的宽度,但是height却没有。下面是我的代码:

代码语言:javascript
复制
@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:

代码语言:javascript
复制
    <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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-11-20 02:30:50

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

代码语言:javascript
复制
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被设置为填充父级,parent是一个不会在其子级上强加维度的ScrollView。我不知道您要对那里的ScrollView做什么(特别是当您希望表格填满整个屏幕时),所以我不知道该推荐您什么。此外,我不知道你的完整布局,但你的嵌套到许多布局。

票数 6
EN

Stack Overflow用户

发布于 2012-11-19 06:01:05

对于每个表格行集合高度:

代码语言:javascript
复制
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));
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13444692

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档