mainactivity.java
package com.example.codeui1;import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
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;@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MainActivity extends Activity {
public TextView text2;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout frameLayout=new FrameLayout(this);
frameLayout.setBackground(this.getResources().getDrawable(R.drawable.ic_launcher));
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.BOTTOM;
text2.setLayoutParams(params);
text2.setOnClickListener(new OnClickListener(){ @Override
public void onClick(View v) {
new Builder(MainActivity.this).setTitle("警告信息")
.setMessage("自我毁灭程序即将启动,确定启动?")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO 自动生成的方法存根
Log.i("1.1","进入游戏了");
}
}).setNegativeButton("QUIT",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO 自动生成的方法存根
Log.i("1.1","离开游戏了");
finish();
}
}).show();
// TODO 自动生成的方法存根
}
});
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;
}}
用java代码控制Ui界面中的控件布局总是不成功,控件的布局总是默认的,控件布局的代码已经用红色字体标出,请问问题出在哪里了布局控件
package com.example.codeui1;import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
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;@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MainActivity extends Activity {
public TextView text2;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout frameLayout=new FrameLayout(this);
frameLayout.setBackground(this.getResources().getDrawable(R.drawable.ic_launcher));
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.BOTTOM;
text2.setLayoutParams(params);
text2.setOnClickListener(new OnClickListener(){ @Override
public void onClick(View v) {
new Builder(MainActivity.this).setTitle("警告信息")
.setMessage("自我毁灭程序即将启动,确定启动?")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO 自动生成的方法存根
Log.i("1.1","进入游戏了");
}
}).setNegativeButton("QUIT",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO 自动生成的方法存根
Log.i("1.1","离开游戏了");
finish();
}
}).show();
// TODO 自动生成的方法存根
}
});
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;
}}
用java代码控制Ui界面中的控件布局总是不成功,控件的布局总是默认的,控件布局的代码已经用红色字体标出,请问问题出在哪里了布局控件
解决方案 »
- 传递list问题
- 腾讯微博客户端发送的数据
- overlay和surfaceflinger的 w_stride 和 w怎么理解阿?
- android 真机调试
- Google开源项目zXing(Android版)扫描二维码(由文本生成的)时偶发性的出现数字
- android中在TextView上显示文字时如何处理对齐
- 请教:判断服务是否运行,那个context出错了
- android 怎么查找一个相同的图块在多张图片中不同的位置
- android源码右上角的menu键怎么去掉
- android手机开发获取手机<<前后左右倾斜>>的角度
- 怎么拯救被阉割了进程的应用
- 为什么我在edittext输入的中文在控制台打印出来都是???这样的 是不是没转为UTF-8
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
这引的不对吧,应该用FrameLayout下的