1 package com.example.ubuntu.myapplication; 2 3 import android.content.DialogInterface; 4 import android.graphics.Color; 5 import android.preference.DialogPreference; 6 import android.support.v7.app.ActionBar; 7 import android.support.v7.app.AlertDialog; 8 import android.support.v7.app.AppCompatActivity; 9 import android.os.Bundle; 10 import android.text.Layout; 11 import android.util.Log; 12 import android.util.TypedValue; 13 import android.view.Gravity; 14 import android.view.View; 15 import android.view.ViewGroup; 16 import android.widget.Button; 17 import android.widget.EditText; 18 import android.widget.FrameLayout; 19 import android.widget.LinearLayout; 20 import android.widget.TextView; 21 22 public class MainActivity extends AppCompatActivity { 23 public TextView text2; 24 25 @Override 26 27 protected void onCreate(Bundle savedInstanceState) { 28 super.onCreate(savedInstanceState); 29 // setContentView(R.layout.activity_main); 30 // setContentView(R.layout.framelayout); 31 FrameLayout frameLayout = new FrameLayout(this); 32 FrameLayout.LayoutParams params=new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 33 ViewGroup.LayoutParams.MATCH_PARENT);//定义框架布局器参数 34 FrameLayout.LayoutParams t1params=new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 35 ViewGroup.LayoutParams.WRAP_CONTENT);//定义显示组件参数 36 FrameLayout.LayoutParams t2params=new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 37 ViewGroup.LayoutParams.WRAP_CONTENT);//定义显示组件参数 38 TextView text1 = new TextView(this); 39 text1.setTextSize(TypedValue.COMPLEX_UNIT_PX,40); 40 text1.setTextColor(Color.rgb(1,1,1)); 41 text1.setText("text1"); 42 text2 = new TextView(this); 43 text2.setText("text2"); 44 text2.setTextSize(TypedValue.COMPLEX_UNIT_PX,24); 45 text2.setTextColor(Color.rgb(1,1,1)); 46 text2.setOnClickListener(new View.OnClickListener() { 47 @Override 48 public void onClick(View v) { 49 new AlertDialog.Builder(MainActivity.this).setTitle("系统提示") 50 .setMessage("游戏有风险,进入需谨慎,真的要进入吗?") 51 .setPositiveButton("确定", 52 new DialogInterface.OnClickListener() { 53 54 @Override 55 public void onClick(DialogInterface dialog, int which) { 56 Log.i("3.2", "进入游戏"); 57 } 58 }).setNegativeButton("退出", 59 new DialogInterface.OnClickListener(){ 60 61 @Override 62 public void onClick(DialogInterface dialog,int which) { 63 Log.i("3.2","退出游戏"); 64 finish(); 65 } 66 }).show(); 67 } 68 }); 69 text1.setGravity(Gravity.LEFT); 70 frameLayout.addView(text1,t1params); 71 // t2params.gravity=Gravity.CENTER; 72 text2.setGravity(Gravity.RIGHT); 73 frameLayout.addView(text2,t1params); 74 setContentView(frameLayout,params); 75 76 /* super.onCreate(savedInstanceState); 77 FrameLayout layout= new FrameLayout(this);//定义框架布局器 78 FrameLayout.LayoutParams params=new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 79 ViewGroup.LayoutParams.MATCH_PARENT);//定义框架布局器参数 80 FrameLayout.LayoutParams tparams=new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 81 ViewGroup.LayoutParams.WRAP_CONTENT);//定义显示组件参数 82 TextView txtview = new TextView(this);//定义组件 83 txtview.setText("欢迎使用框架布局"); 84 EditText et = new EditText(this); 85 et.setText("请选择你喜欢的布局"); 86 Button but=new Button(this); 87 but.setText("按我"); 88 layout.addView(txtview, tparams);//添加组件 89 layout.addView(et, tparams); 90 layout.addView(but, tparams); 91 92 93 setContentView(layout,params);//向屏幕上添加布局显示器 94 */ } 95 }
FrameLayout.LayoutParams t2params=new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);//定义显示组件参数
设置gravity
t2params.gravity=Gravity.CENTER;//有效
text2.setGravity(Gravity.RIGHT);//无效
显示控件:
frameLayout.addView(text1,t1params);
frameLayout.addView(text2,t1params);
setContentView(frameLayout,params);//先addView再显示
显示layout下的activity_main.xml
setContentView(R.layout.activity_main);
用layout下的View资源
FrameLayout frame = (FrameLayout) findViewById(R.id.mylayout);