读书人

tablerow中组件实现竖直排列怎么做

发布时间: 2013-09-06 10:17:17 作者: rapoo

tablerow中组件实现竖直排列,如何做?
下面这段代码动态生成表格1*3表格,每个表格里添加一个按钮和一个文体,运行效果如下图tablerow中组件实现竖直排列,怎么做

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
[解决办法]
tablerow.setOrientation(TableRow.VERTICAL); 只对linearlayout布局有效。


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中。

读书人网 >Android

热点推荐