您的位置:首页 > 移动开发 > Android开发

定义界面布局TextView&Button

2014-04-20 21:45 585 查看
MainActivity.java:

package com.example.layout;

import android.os.Bundle;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.text.Layout;
import android.view.Menu;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView text1 = new TextView(MainActivity.this);
text1.setText("zhengzhixiong");

LinearLayout layout = new LinearLayout(MainActivity.this);
layout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
//  Button btn3 = new Button(getApplicationContext());
//  btn3.setText("Button3");
Button btn1 = new Button(getApplication());
btn1.setText("Button1");
Button btn2 = new Button(getBaseContext());
btn2.setText("Button2");
Button btn3 = new Button(this);
btn3.setText("Button3");

//  Button btn4 = new Button(getParent());
//不能运行
//  btn4.setText("Button4");
//不能运行

layout.addView(text1,p);
//   layout.addView(btn3);
//不能运行
layout.addView(btn1);
layout.addView(btn2);
layout.addView(btn3);
//  layout.addView(btn4);
//不能运行
// layout.addView(btn5);
//不能运行
this.setContentView(layout);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}


 

效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 布局 textview
相关文章推荐