安卓开发GridLayout设计计算器界面显示不全
publicclassGridLayoutTestextendsActivity{GridLayoutgridLayout;String[]chars=newString...
public class GridLayoutTest extends Activity{ GridLayout gridLayout; String[] chars=new String[]{ "7","8","9","÷", "4","5","6","×", "1","2","3","-", ".","0","=","+" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.grid_larout); gridLayout=(GridLayout)findViewById(R.id.root); for(int i=0;i<chars.length;i++){ Button bn=new Button(this); bn.setText(chars[i]); bn.setTextSize(40); GridLayout.Spec rowSpec=GridLayout.spec(i/4+2); GridLayout.Spec columnSpec=GridLayout.spec(i % 4); String msg="row:"+(i/4+2)+" col:"+(i%4); Log.e("tag",msg); GridLayout.LayoutParams params=new GridLayout.LayoutParams(rowSpec,columnSpec); // params.setGravity(Gravity.FILL); gridLayout.addView(bn,params); } }
展开
5个回答
展开全部
这个我刚才才写过,然后设置字体的地方有一点小问题,我贴上我的代码你看一下
package com.huangjun.brioal.caculatorui;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.GridLayout;
public class MainActivity extends ActionBarActivity {
private String[] chars = new String[] {
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", ".", "=", "+"
};
private GridLayout gridLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridLayout = (GridLayout) findViewById(R.id.root);
for (int i = 0; i < chars.length; i++) {
Button btn = new Button(this);
btn.setText(chars[ i ]);
btn.setTextSize(80);
// 指定所在的行
GridLayout.Spec row = GridLayout.spec(i / 4 + 2);
// 指定所在的列
GridLayout.Spec column = GridLayout.spec(i % 4);
GridLayout.LayoutParams params = new GridLayout.LayoutParams(row, column);
// 指定填满父容器
params.setGravity(Gravity.FILL);
// 添加按钮
gridLayout.addView(btn,params);
}
}、
}
顶上的我的截图,有一点字体大小小问题但是不想弄了
2015-07-09
展开全部
这跟你的xml文件有关,贴出来看看
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
安卓开发GridLayout设计计算器界面显示不全这个没有关系。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xys.myapplication/com.xys.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridLayout.addView(android.view.View, android.view.ViewGroup$LayoutParams)' on a null object reference
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询