Android 重用Layout Common action

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
	android:layout_width="fill_parent" android:layout_height="fill_parent"
	xmlns:android="http://schemas.android.com/apk/res/android">
	<Button android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:id="@+id/Button11"
		android:text="Activity 1"></Button>
	<Button android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:id="@+id/Button12"
		android:text=" Activity 2   "></Button>
</LinearLayout>


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/RelativeLayout01"
	android:layout_width="fill_parent" android:layout_height="fill_parent"
	xmlns:android="http://schemas.android.com/apk/res/android">
	<TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:background="#ffffff"
		android:text="Activity -- 1          "></TextView>
	<RelativeLayout android:id="@+id/RelativeLayout02"
		android:layout_width="wrap_content" android:layout_height="wrap_content"
		android:layout_centerInParent="true">
	</RelativeLayout>
	<com.farhan.masterLayout.Footer
		android:id="@+id/layoutFooter" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:layout_alignParentBottom="true" />
</RelativeLayout>


public class Activity1 extends Activity {

	Footer footer;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		footer = (Footer) findViewById(R.id.layoutFooter);
		footer.setActivity(this);

	}

}


public class Footer extends LinearLayout {
	private Context mContext;
	private Button btn11;
	private Button btn12;
	// use Activity Object to call finish() method which is not possible using
	// context
	private Activity mActivity;

	public Footer(Context context, AttributeSet attrs) {
		super(context, attrs);

		mContext = context;

		String infService = Context.LAYOUT_INFLATER_SERVICE;
		LayoutInflater li;

		li = (LayoutInflater) getContext().getSystemService(infService);
		li.inflate(R.layout.footer, this, true);

		btn11 = (Button) findViewById(R.id.Button11);
		btn11.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent myIntent = new Intent(mContext, Activity1.class);
				mActivity.finish();
				mActivity.startActivity(myIntent);
			}

		});

		btn12 = (Button) findViewById(R.id.Button12);
		btn12.setOnClickListener(mFooterListener);

	}

	public void setActivity(Activity activity) {
		// set init otherwise of ctor and call externally...
		mActivity = activity;
	}

	// Create an anonymous implementation of OnClickListener
	private OnClickListener mFooterListener = new OnClickListener() {
		public void onClick(View v) {
			Intent myIntent;
			switch (v.getId()) {
			case R.id.Button12:
				myIntent = new Intent(mContext, Activity2.class);
				mActivity.finish();
				mActivity.startActivity(myIntent);
				break;
			}

		}
	};
}


Here  its complete source code. http://www.megaupload.com/?d=QWCP60GN

你可能感兴趣的:(android)