定义界面布局TextView&Button

MainActivity.java:

package com.example.layout;

import android.os.Bundle;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.text.Layout;
import android.view.Menu;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView text1 = new TextView(MainActivity.this);
        text1.setText("zhengzhixiong");
        
        LinearLayout layout = new LinearLayout(MainActivity.this);
        layout.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(       
        		                   LinearLayout.LayoutParams.FILL_PARENT,       
        		                   LinearLayout.LayoutParams.WRAP_CONTENT       
        		); 
     //  Button btn3 = new Button(getApplicationContext());
     //  btn3.setText("Button3");  
       Button btn1 = new Button(getApplication());
       btn1.setText("Button1");
       Button btn2 = new Button(getBaseContext());
       btn2.setText("Button2");
       Button btn3 = new Button(this);
       btn3.setText("Button3");
       
     //  Button btn4 = new Button(getParent());   
       //不能运行
     //  btn4.setText("Button4");
       //不能运行
       
       layout.addView(text1,p);
    //   layout.addView(btn3);
       //不能运行
       layout.addView(btn1);
       layout.addView(btn2);
       layout.addView(btn3);
     //  layout.addView(btn4);
       //不能运行
      // layout.addView(btn5);
       //不能运行
        this.setContentView(layout);
    }


    @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;
    }
    
}


 

效果:

定义界面布局TextView&Button_第1张图片

你可能感兴趣的:(Android)