Layout布局问题
我的布局是要一个表格, 共4个格子(2行2列), 每格需求宽度不一样, 高度一样.占屏宽比例: 1格占40%, 2格占60%, 3格占20%,4格占80%.
以下的代码不能达效果, 麻烦各位大侠帮助.
//java
TextView tv1 = (TextView)findViewById(R.id.tv1);
TextView tv2 = (TextView)findViewById(R.id.tv2);
TextView tv3 = (TextView)findViewById(R.id.tv3);
TextView tv4 = (TextView)findViewById(R.id.tv4);
Display display = this.getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int hight = display.getHeight();
tv1.setWidth((int) ((width)*0.4));
tv2.setWidth((int) ((width)*0.6));
tv3.setWidth((int) ((width)*0.2));
tv3.setWidth((int) ((width)*0.8));
tv1.setHeight((int) ((hight)*0.25));
tv2.setHeight((int) ((hight)*0.25));
tv3.setHeight((int) ((hight)*0.25));
tv4.setHeight((int) ((hight)*0.25));
//xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout001"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="bottom|center_horizontal"
android:orientation="horizontal" >
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:orientation="horizontal" >
<TableRow>
<TextView
android:id="@+id/tv1"
android:layout_margin="2dip"
android:background="#00FA9A"
android:gravity="right"
android:text="T1"
android:textColor="#0000FF"
android:textSize="38sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/tv2"
android:layout_margin="2dip"
android:background="#00FA9A"
android:gravity="center"
android:text="T2"
android:textColor="#0000FF"
android:textSize="30sp"
android:textStyle="bold" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/tv3"
android:layout_margin="2dip"
android:background="#00FFFF"
android:gravity="right"
android:text="T3"
android:textColor="#000000"
android:textSize="30sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/tv4"
android:layout_margin="2dip"
android:background="#FFFFFF"
android:gravity="center"
android:text="T4"
android:textColor="#000000"
android:textSize="30sp"
android:textStyle="bold" />
</TableRow>
</TableLayout>
</LinearLayout> layout --------------------编程问答-------------------- 在layout里使用layout_weight来设置就可以了,参考:
http://www.cnblogs.com/alpha-bowen/archive/2011/03/02/1969343.html --------------------编程问答-------------------- 我需要自定义textview所占屏宽的比例, 如18.5% --------------------编程问答-------------------- TableLayout 的布局方式你可以认为和HTML中的Table一样。
如果要不等比例,通过layout_span的方式合并列。
你可以定义成10列,然后在定义你的百分比折算后占几列,而且不用再获取屏幕宽度和高度了。
补充:移动开发 , Android