一:xml布局文件控制UI界面
activity_main.xml
<FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/title"
style="@style/text"/>
<TextView
android:id="@+id/startButton"
android:layout_gravity="center_vertical|center_horizontal"
android:text="@string/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/text"
/>
</FrameLayout>
strings.xml
<?xmlversion="1.0"encoding="utf-8"?>
<resources>
<stringname="app_name">Android1</string>
<stringname="action_settings">Settings</string>
<stringname="hello_world">Hello world!</string>
<stringname="title">使用xml布局文件控制UI界面</string>
<stringname="start">单击开始游戏</string>
</resources>
styles.xml
<stylename="text">
<itemname="android:textSize">24px</item>
<itemname="android:textColor">#111111</item>
</style>
二:在代码中控制UI界面
@SuppressLint("NewApi")
publicclass MainActivity extends Activity {
public TextViewtext2;
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FrameLayout framelayout=new FrameLayout(this);
setContentView(framelayout);
TextView text1=new TextView(this);
text1.setText("在代码中控制UI界面");
//设置文字大小,单位像素
text1.setTextSize(TypedValue.COMPLEX_UNIT_PX, 24);
text1.setTextColor(Color.rgb(1, 1, 1));
framelayout.addView(text1);
text2=new TextView(this);
text2.setText("单击进入游戏...");
text2.setTextSize(TypedValue.COMPLEX_UNIT_PX,24);
text2.setTextColor(Color.rgb(1, 1, 1));
LayoutParams params=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.gravity=Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL;
text2.setLayoutParams(params);
text2.setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View v) {
new AlertDialog.Builder(MainActivity.this).setTitle("系统提示")
.setMessage("真的要进入游戏吗")
.setPositiveButton("确定",new DialogInterface.OnClickListener() {
@Override
publicvoid onClick(DialogInterface dialog, int which) {
// TODO Auto-generatedmethod stub
Log.i("3.2","进入游戏");
};
}).setNegativeButton("退出",new DialogInterface.OnClickListener() {
@Override
publicvoid onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generatedmethod stub
Log.i("3.2","退出游戏");
finish();
}
}).show();
}
});
framelayout.addView(text2);
}
@Override
publicboolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; thisadds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
returntrue;
}
}
版权声明:博主原创文章,转载请说明出处。http://blog.csdn.net/dzy21