我的布局是要一个表格, 共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
占屏宽比例: 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
解决方案 »
- 怎么样调用android系统的照相机 ?
- android spinner setOnItemSelectedListener的问题
- 请教:java线程同步
- EPSON TX400 连供
- 刚出炉的编辑图片利器!才上线就冲百啦!支持者有奖励哦
- Activity的跳转问题 intent的携带数据
- 急!急!急!JNI传递嵌套对象数组的问题
- 字符串转化日期的问题
- android客户端无法连接到openfire服务器
- 从MainActivity中启动一个Service,为什么没有反应?
- Choreographer的一个问题
- sencha touch 做的项目打包到 Android ,在 4.0 + 机器上,输入框 焦点时 有黄色边框,怎么去掉
http://www.cnblogs.com/alpha-bowen/archive/2011/03/02/1969343.html
如果要不等比例,通过layout_span的方式合并列。
你可以定义成10列,然后在定义你的百分比折算后占几列,而且不用再获取屏幕宽度和高度了。