MainActivity。java 代码如下:
package com.example.andrew;import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.util.Log;
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;
public class MainActivity extends Activity {
public TextView text2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout frameLayout=new FrameLayout(this);
frameLayout.setBackgroundDrawable(this.getResources().
getDrawable(TRIM_MEMORY_UI_HIDDEN));//设置背景
setContentView(frameLayout);
TextView text1=new TextView(this);
text1.setText("在代码中控制UI界面");
text1.setTextSize(TypedValue.COMPLEX_UNIT_PX, 24);
text1.setTextColor(Color.CYAN);
frameLayout.addView(text1);
text2=new TextView(this);
text2.setText("单击进入游戏界面。");
text2.setTextSize(TypedValue.COMPLEX_UNIT_PX, 24);
text2.setTextColor(Color.GREEN);
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("游戏有风险")
.setNegativeButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Log.i("android","进入游戏");
}
}).setNegativeButton("退出",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Log.i("android", "退出游戏");
finish();
}
}).show();
}
});
frameLayout.addView(text2);
} @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;
}}
解决方案 »
- libcore.java.io.InterruptedStreamTest#testInterruptWritablePipeChannel测试项未通过
- 从log中截的一段,求大神知道错误在哪
- android模拟器无法启动
- android开发java平台问题
- Android平台入门到精通
- android中的Gps服务怎么封装出来
- android原生开发可以使用js+css来做界面吗?
- 小白疑问:.so文件和驱动程序的区别
- zirco 浏览器 安卓版的 浏览isapi扩展 中文乱码 请问怎么解决
- camera使用GLSurfaceView问题
- android真机调试,怎么获取读取文件权限
- android 图表控件问题
1.this.getResources().getDrawable(TRIM_MEMORY_UI_HIDDEN),这里的TRIM_MEMORY_UI_HIDDEN未定义,所以你的apk挂掉了。这个参数一般都是R.drawable.****,****就是你放在res/drawable/目录下的****.png图片。
2.frameLayout.setBackgroundDrawable()这个方法已经不推荐用了,建议用setBackground()。
3.你用FrameLayout,但是两个TextView之间又没有设置padding,导致两个text叠加显示在一起。建议换LinearLayout垂直布局,或者还是用FrameLayout,设置TextView的间距。