标签:android
一:xml布局文件控制UI界面
activity_main.xml
<FrameLayout xmlns: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
<?xml version="1.0"encoding="utf-8"?>
<resources>
<string name="app_name">Android1</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="title">使用xml布局文件控制UI界面</string>
<string name="start">单击开始游戏</string>
</resources>
styles.xml
<style name="text">
<item name="android:textSize">24px</item>
<item name="android:textColor">#111111</item>
</style>
二:在代码中控制UI界面
@SuppressLint("NewApi")
public class MainActivity extends Activity {
public TextView text2;
@Override
protected void 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
public void onClick(View v) {
new AlertDialog.Builder(MainActivity.this).setTitle("系统提示")
.setMessage("真的要进入游戏吗")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generatedmethod stub
Log.i("3.2", "进入游戏");
};
}).setNegativeButton("退出", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generatedmethod stub
Log.i("3.2", "退出游戏");
finish();
}
}).show();
}
});
framelayout.addView(text2);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; thisadds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
版权声明:博主原创文章,转载请说明出处。http://blog.csdn.net/dzy21
标签:android
原文地址:http://blog.csdn.net/dzy21/article/details/47321459