android程序怎么样自动适应屏幕大小

Android 码拜 8年前 (2016-05-05) 921次浏览
xml:
<?xml version=”1.0″ encoding=”utf-8″ ?>
<TableLayout
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”
android:rowCount=”10″
android:columnCount=”4″
android:id=”@+id/mainwindow”
>
<TextView
android:id=”@+id/show”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:scrollbars=”vertical”
android:singleLine=”false”
android:textSize=”50sp”
android:layout_gravity=”right”
android:background=”#000″
android:textColor=”@color/red”
android:gravity=”right”
/>
</TableLayout>
java:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculator);
tableLayout = (TableLayout) findViewById(R.id.mainwindow);
text = (TextView) findViewById(R.id.show);
text.setMovementMethod(ScrollingMovementMethod.getInstance());
text.setLines(2);
Button[] bn = new Button[20];
tableLayout.setStretchAllColumns(true);
tableLayout.setShrinkAllColumns(true);
int n = 0;
for (int i = 0; i < 5; i++) {
TableRow textrow = new TableRow(this);
for (int j = 0; j < 4; j++) {
bn[n] = new Button(this);
bn[n].setText(chars[n]);
bn[n].setTextSize(30);
TableRow.LayoutParams lp = new TableRow.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
textrow.addView(bn[n], lp);
n++;
}
tableLayout.addView(textrow);
}
}
问一下下怎么样让android程序自动适应各种屏幕大小,这个程序在虚拟机上可以自动适应屏幕大小,但是一安装到手机上面就不能自动适应屏幕大小
解决方案

20

TableRow.LayoutParams lp = new TableRow.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

这句改成:

TableRow.LayoutParams lp = new TableRow.LayoutParams(
                0, LayoutParams.WRAP_CONTENT, 1);

题主试试

20

以效果图为标准,每次来计算各个组件的大小,例如width = width*(效果图width/实际屏幕width)
高度也是一样

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明android程序怎么样自动适应屏幕大小
喜欢 (0)
[1034331897@qq.com]
分享 (0)