标签:android style blog http io color ar os 使用
一、使用XML布局文件控制UI界面
res\layout\activity_main.xml代码如下:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/FrameLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/title"
style="@style/text"
>
</TextView>
<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"
>
</TextView>
</FrameLayout>
一个TextView组件用于显示提示文字,一个在窗体正中间显示开始游戏按钮
其中res\values\styles.xml代码如下
<resources xmlns:android="http://schemas.android.com/apk/res/android"> <style name="text"> <item name="android:textSize">24dp</item> <item name="android:textColor">#111111</item> </style> </resources>
用于指定应用的样式,指定文字的大小和颜色。
最后在主活动中也就是MainActivity中,应用一下代码指定应用的布局文件。
setContentView(R.layout.activity_main);
二、用代码来控制UI界面
package com.basil_lee.ui;
import android.os.Bundle;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
FrameLayout frameLayout=new FrameLayout(this);//创建帧布局管理器
frameLayout.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.background));//设置背景图片
setContentView(frameLayout);//设置在Activity中显示frameLayout
//创建一个TextView组件
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);
//创建另外一个TextView组件
TextView text2=new TextView(this);
text2.setText("单击进入游戏。。。。。。");
text2.setTextSize(TypedValue.COMPLEX_UNIT_PX,50);//设置文字的大小,单位为像素
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组件添加单击事件,并将组件添加到布局管理器中
text2.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
new AlertDialog.Builder(MainActivity.this).setTitle("系统提示")
.setMessage("游戏有风险,进入需谨慎,真的要进入吗?")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this, "进入游戏", Toast.LENGTH_LONG).show();
}
}).setNegativeButton("退出",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this, "退出游戏", Toast.LENGTH_LONG).show();
finish();
}
}).show();
}
});
frameLayout.addView(text2);
}
}
后面继续更新。。。。。
标签:android style blog http io color ar os 使用
原文地址:http://www.cnblogs.com/BasilLee/p/4084214.html