activity 中写入布局文件

程序代码如下

package com.android.demo;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

/* HeaderContentFooterGUI
 * 
 * Create a sample GUI with a header bar, scroll-able content and a footer
 * 
 * (C) 2011 Radu Motisan, [email protected]
 * www.pocketmagic.net
 * All rights reserved.
 */

public class MyTestActivity extends Activity implements OnClickListener{
	
	final static int		idTopLayout = Menu.FIRST + 100,
							idBack 		= Menu.FIRST + 101,
							idBotLayout	= Menu.FIRST + 102;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        //Hide titlebar
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        //Create our top content holder
        RelativeLayout global_panel = new RelativeLayout (this);
		global_panel.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		global_panel.setGravity(Gravity.FILL);
		
		// +++++++++++++ TOP COMPONENT: the header
		RelativeLayout ibMenu = new RelativeLayout(this);
     	ibMenu.setId(idTopLayout);
		ibMenu.setBackgroundDrawable(getResources().getDrawable(R.drawable.line));
     	int ibMenuPadding = (int) 6;
     	ibMenu.setPadding(ibMenuPadding,ibMenuPadding,ibMenuPadding,ibMenuPadding);
     	RelativeLayout.LayoutParams topParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
     	topParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
     	global_panel.addView(ibMenu,topParams);
     	// textview in ibMenu : card holder
		TextView cTV = new TextView(this);
		cTV.setText("Header");
		cTV.setTextColor(Color.rgb(255,255,255));
		cTV.setTextSize(18);
		cTV.setTypeface(Typeface.create("arial", Typeface.BOLD));
		RelativeLayout.LayoutParams lpcTV = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		lpcTV.addRule(RelativeLayout.CENTER_IN_PARENT);
		ibMenu.addView(cTV, lpcTV);
		// cancel button in ibMenu
		Button m_bCancel = new Button(this);
		m_bCancel.setId(idBack);
		m_bCancel.setOnClickListener((OnClickListener) this);
		m_bCancel.setText("Cancel");
		m_bCancel.setTextSize(12);
		m_bCancel.setTypeface(Typeface.create("arial", Typeface.NORMAL));
		RelativeLayout.LayoutParams lpbCancel = 
			new RelativeLayout.LayoutParams(100,50);//LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		lpbCancel.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
		lpbCancel.addRule(RelativeLayout.CENTER_VERTICAL);
		ibMenu.addView(m_bCancel, lpbCancel);

		// +++++++++++++ BOTTOM COMPONENT: the footer
		RelativeLayout ibMenuBot = new RelativeLayout(this);
		ibMenuBot.setId(idBotLayout);
		ibMenuBot.setBackgroundDrawable(getResources().getDrawable(R.drawable.line));
		ibMenuBot.setPadding(ibMenuPadding,ibMenuPadding,ibMenuPadding,ibMenuPadding);
		RelativeLayout.LayoutParams botParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		botParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
		global_panel.addView(ibMenuBot,botParams);
		// textview in ibMenu : card holder
		TextView cTVBot = new TextView(this);
		cTVBot.setText("www.pocketmagic.net");
		cTVBot.setTextColor(Color.rgb(179,116,197));
		cTVBot.setTextSize(12);
		cTVBot.setTypeface(Typeface.create("arial", Typeface.BOLD));
		RelativeLayout.LayoutParams lpcTVBot = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		lpcTVBot.addRule(RelativeLayout.CENTER_IN_PARENT);
		ibMenuBot.addView(cTVBot, lpcTVBot);
     	
		// +++++++++++++ MIDDLE COMPONENT: all our GUI content
		LinearLayout midLayout = new LinearLayout (this);
		midLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
		midLayout.setOrientation(LinearLayout.VERTICAL);
		RelativeLayout.LayoutParams midParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		midParams.addRule(RelativeLayout.ABOVE,ibMenuBot.getId());
		midParams.addRule(RelativeLayout.BELOW,ibMenu.getId());
		global_panel.addView(midLayout,midParams );
		//scroll - so our content will be scrollable between the header and the footer
		ScrollView vscroll = new ScrollView(this);
		vscroll.setFillViewport(false);
		midLayout.addView(vscroll);
		//panel in scroll: add all controls/ objects to this layout
		LinearLayout m_panel = new LinearLayout (this);
		m_panel.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
		m_panel.setOrientation(LinearLayout.VERTICAL);
		vscroll.addView(m_panel);
		
		Button b[] = new Button[10];
		for (int i=0;i<10;i++) {
			b[i] = new Button(this);
			b[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			b[i].setText("But "+i);
			m_panel.addView(b[i]);
		}
        setContentView(global_panel);
    }

	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		int id = arg0.getId();
		
		// If cancel is pressed, close our app
		if (id == idBack) finish();
		
	}
}
程序效果如图


你可能感兴趣的:(activity 中写入布局文件)