如何设置动态创建的tablelayout单元格高度&宽度固定

编程入门 行业动态 更新时间:2024-10-10 15:23:23
本文介绍了如何设置动态创建的tablelayout单元格高度&宽度固定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是android的初学者。如何设置动态创建的表格布局单元格高度&宽度固定。 点击此处 [ ^ ]截图 细胞高度&宽度根据文字大小而变化。我需要将所有单元格大小固定。 XML代码是

I am beginner in android. How to set dynamically created table layout cell height & width fixed. Click here[^] for screenshot The cell height & width was changing based on text size. i need all cell size as fixed. XML code is

<LinearLayout xmlns:android="schemas.android/apk/res/android" android:id="@+id/main" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:weightSum="100" > </LinearLayout>

Java代码

Java Code

Main = (LinearLayout) findViewById(R.id.main); Main_1 = new LinearLayout(this); LinearLayout.LayoutParams sudoku_1_lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT); Main_1.setLayoutParams(sudoku_1_lp); Main_1.setWeightSum(100); Main_1.setOrientation(LinearLayout.VERTICAL); Main_1.setId(100); //Layout for sudoku_board sudokuboard = new LinearLayout(this); LinearLayout.LayoutParams sudokuboard_lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,0,80.0f); sudokuboard_lp.setMargins(1, 1, 1, 1); sudokuboard.setLayoutParams(sudokuboard_lp); sudokuboard.setWeightSum(80); sudokuboard.setOrientation(LinearLayout.VERTICAL); sudokuboard.setBackgroundColor(Color.parseColor("#FF0000")); sudokuboard.setId(102); tblsudoku = new TableLayout(this); TableLayout.LayoutParams sudoku_tbl_lp = new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.FILL_PARENT,80.0f); sudoku_tbl_lp.setMargins(2, 2, 2, 2); tblsudoku.setLayoutParams(sudoku_tbl_lp); tblsudoku.setId(103); for(int i=0; i < 9; i++) { TableRow NewRow1 =new TableRow(this); TableLayout.LayoutParams n1 = new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,0,10.0f); if(i==3 || i==6 ) { n1.setMargins(0,2, 0, 0); } else { n1.setMargins(0,1, 0, 0); } NewRow1.setLayoutParams(n1); for(int j=0; j<9; j++) { TextView tv_00 = new TextView(this); int id=10*i; id=id+j; id=id*id*id; String strText=""+id; tv_00.setText(strText); TableRow.LayoutParams r1 = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.FILL_PARENT, 10.0f); if(j==2 || j==5 ) { r1.setMargins(0, 0, 2, 0); } else { r1.setMargins(0, 0, 1, 0); } tv_00.setLayoutParams(r1); tv_00.setGravity(Gravity.CENTER); tv_00.setBackgroundColor(Color.GREEN); tv_00.setId(id); tv_00.setPadding(2, 2, 2, 2); NewRow1.addView(tv_00); } tblsudoku.addView(NewRow1); } sudokuboard.addView(tblsudoku); Main_1.addView(sudokuboard); Main.addView(Main_1);

推荐答案

您是否尝试为表格布局设置固定值? Have you tried to set fixed value for the table layout? Display display = getWindowManager().getDefaultDisplay(); Point outSize = new Point(); display.getSize(outSize); TableLayout.LayoutParams sudoku_tbl_lp = new TableLayout.LayoutParams(outSize.x,outSize.y,80.0f);

首先你不要需要weightSum属性。只需给每一行和每个单元重量相同,例如1. 然后给每一行的高度为0,每个单元格(TextView)的宽度为0. First of all you do not need the weightSum attribute. Just give every row and cell the same weight, e.g. 1. Then give every row a height of 0, and every cell (TextView) a width of 0.

更多推荐

如何设置动态创建的tablelayout单元格高度&amp;宽度固定

本文发布于:2023-11-23 01:05:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1619553.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:宽度   单元格   如何设置   高度   动态

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!