0
点赞
收藏
分享

微信扫一扫

【Android开发】用户界面设计-在代码中控制UI界面


效果图:

【Android开发】用户界面设计-在代码中控制UI界面_界面设计

【Android开发】用户界面设计-在代码中控制UI界面_android开发_02

实现方法:

MainActivity:

package com.example.test;


import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;


public class MainActivity extends Activity {
public TextView text2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//使用代码布局就不用设置下面这句
//setContentView(R.layout.main);

FrameLayout frameLayout=new FrameLayout(this);//创建帧布局管理器
//设置背景
frameLayout.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.backgroud));
//设置在Activity中显示frameLayout
setContentView(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);//将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) {
Log.i("3.2", "进入游戏");//输出消息日志
}
}).setNegativeButton("退出",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.i("3.2", "退出游戏");//输出消息日志
finish();
}
}).show();
}
});
frameLayout.addView(text2);
}
}


main.xml设置frameLayout布局就可以了

举报

相关推荐

0 条评论