下面这段代码动态生成表格1*3表格,每个表格里添加一个按钮和一个文体,运行效果如下图java代码如下: TableLayout tableLayout = (TableLayout)findViewById(R.id.TableLayout01);
tableLayout.setStretchAllColumns(true);
for (int row=0;row<1;row++){
TableRow tablerow = new TableRow(MyLocationUser.this);
tablerow.setBackgroundColor(Color.rgb(222, 220, 210));
tablerow.setOrientation(TableRow.VERTICAL);
for (int col=0;col<3;col++){
Button bt = new Button(MyLocationUser.this);
bt.setText("按钮");
tablerow.addView(bt);
TextView tv= new TextView(MyLocationUser.this);
tv.setText("文本");
tablerow.addView(tv);
}
tableLayout.addView(tablerow,new TableLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
xml代码如下:<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/bt_dttable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="动态生成表格"
android:onClick="dttable"
/>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:background="#dedcd2">
<TableRow
android:background="#dedcd2"
android:layout_margin="0.5dip">
<TextView
android:text=""
android:textSize="20dip"
android:textStyle="bold"
/>
<TextView
android:text=""
android:textSize="20dip"
android:textStyle="bold"
/>
<TextView
android:text=""
android:textSize="20dip"
android:textStyle="bold"
/>
</TableRow>
</TableLayout>
<TableLayout
android:id="@+id/TableLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:background="#dedcd2"
>
<TableRow
android:orientation="vertical"
android:background="#dedcd2"
android:layout_margin="0.5dip"
>
<TextView
android:text=""
android:textSize="20dip"
android:textStyle="bold"
android:background="#ffffff"
android:layout_margin="1dip"
/>
</TableRow>
</TableLayout>
</LinearLayout>
</ScrollView>
现在我需要让文本在按钮的下面(也就是组件竖直排列),而不是右面(水平排列),在java代码中已使用了tablerow.setOrientation(TableRow.VERTICAL);
但是没有作用,请问,tablerow中组件实现竖直排列,如何做?谢谢!
tablerow排列方式vertical
tableLayout.setStretchAllColumns(true);
for (int row=0;row<1;row++){
TableRow tablerow = new TableRow(MyLocationUser.this);
tablerow.setBackgroundColor(Color.rgb(222, 220, 210));
tablerow.setOrientation(TableRow.VERTICAL);
for (int col=0;col<3;col++){
Button bt = new Button(MyLocationUser.this);
bt.setText("按钮");
tablerow.addView(bt);
TextView tv= new TextView(MyLocationUser.this);
tv.setText("文本");
tablerow.addView(tv);
}
tableLayout.addView(tablerow,new TableLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
xml代码如下:<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/bt_dttable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="动态生成表格"
android:onClick="dttable"
/>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:background="#dedcd2">
<TableRow
android:background="#dedcd2"
android:layout_margin="0.5dip">
<TextView
android:text=""
android:textSize="20dip"
android:textStyle="bold"
/>
<TextView
android:text=""
android:textSize="20dip"
android:textStyle="bold"
/>
<TextView
android:text=""
android:textSize="20dip"
android:textStyle="bold"
/>
</TableRow>
</TableLayout>
<TableLayout
android:id="@+id/TableLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:background="#dedcd2"
>
<TableRow
android:orientation="vertical"
android:background="#dedcd2"
android:layout_margin="0.5dip"
>
<TextView
android:text=""
android:textSize="20dip"
android:textStyle="bold"
android:background="#ffffff"
android:layout_margin="1dip"
/>
</TableRow>
</TableLayout>
</LinearLayout>
</ScrollView>
现在我需要让文本在按钮的下面(也就是组件竖直排列),而不是右面(水平排列),在java代码中已使用了tablerow.setOrientation(TableRow.VERTICAL);
但是没有作用,请问,tablerow中组件实现竖直排列,如何做?谢谢!
tablerow排列方式vertical
解决方案 »
- android4.1.2的日历
- 大伙帮忙看看 内存溢出导致android重启。
- 求解!如何在edittext中预设文字
- 【------------安卓和JAVA区别---------------】
- 请用过VideoView类的朋友帮助
- Android短信数据库sms的字段person为什么一直是空?
- 需求中出现 星期 的需求,怎么搞
- gallery android:targetSdkVersion view显示不同
- Android播放器问题!急,在线等~~
- 怎么实现异步加载LISTVIEW中的imageview上的图片啊
- ListView 搭配 CheckBox 全选超过一屏幕数据后出错
- 精通Android4.0开发视频(适合新手学习)
for (int col=0;col<3;col++){
Button bt = new Button(MyLocationUser.this);
bt.setText("按钮");
tablerow.addView(bt);
TextView tv= new TextView(MyLocationUser.this);
tv.setText("文本");
tablerow.addView(tv);}
加一层linearlayout,把button和textview放在linearlayout中。