Ara*_*yan 2 android tablerow layoutparams android-4.0-ice-cream-sandwich android-tablelayout
例如,我以编程方式创建了4个表格,我希望每个表格高度为屏幕的1/4大小.每个桌子高度是屏幕尺寸的一半.我尝试了不同的方法,但是桌子高度并没有改变.我管理更改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;
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
你没有使用正确的LayoutParams,对于表 TableLayout,你应该设置LinearLayout.LayoutParams(如添加表到LinearLayout),并为您TableRows的LayoutParams应该是TableLayout.LayoutParams(因为他们的孩子TableLayout).即使进行了上述更改,事情也无法奏效,因为它TableLayout是一个对其子项施加约束的小部件,最值得注意的是高度将自动设置为WRAP_CONTENTfor TableRows.改变这种情况的唯一方法是使用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);
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为你TableLayout的设置是填充父级,父级是一个ScrollView不对其子级施加维度的父级.我不知道你在ScrollView那里尝试做什么(尤其是你希望桌子填满整个屏幕)所以我不知道该推荐你什么.此外,我不知道你的完整布局,但你的嵌套到很多布局.
| 归档时间: |
|
| 查看次数: |
6138 次 |
| 最近记录: |