Android应用开发之编码实现 软件界面

来源:互联网 发布:mac装virtualbox 编辑:程序博客网 时间:2024/05/21 04:43

删掉了main.xml文件

首先使用线性布局

LinearLayout linearLayout=new LinearLayout(this);

设置布局方向:

linearLayout.setOrientation(LinearLayout.VERTICAL);

布局的参数:填充整个窗口的宽和高

LinearLayout.LayoutParams layoutParams=new LinearLayout.LinearParams(ViewGroup.LinearParams.HILL_PARENT,ViewGroup.LinearParams.FILL_PARENT);

建立一个textview的布局参数:

ViewGroup.LayoutParams textViewParams=new ViewParams.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT)

建立一个textview

TextView textView=new TextView(this);textView.setText(R.string.app_name);textView.setId(20);

往容器里填加控件:

linearLayout.addView(text.View,textViewParams);setContentView(linearLayout,layoutParams);

只有界面在运行期。随着运行期的某些条件而变化的


实验:

 private static final int HELLO_TV_ID = 10;private LayoutParams params;public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);//        setContentView(R.layout.main);                LinearLayout linearLayout = new LinearLayout(this);        TextView textView = new TextView(this);        textView.setText(R.string.hello);        textView.setId(HELLO_TV_ID);                params = new LayoutParams(LayoutParams.FILL_PARENT,                                        LayoutParams.WRAP_CONTENT);        linearLayout.addView(textView, params);                params = new LayoutParams(LayoutParams.FILL_PARENT,                                       LayoutParams.FILL_PARENT);        this.setContentView(linearLayout,params);    }






原创粉丝点击