ActivityMain.java
package com.android.layout;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ActivityMain extends Activity {
/** Called when the activity is first created. */
OnClickListener listener0 = null;
OnClickListener listener1 = null;
OnClickListener listener2 = null;
OnClickListener listener3 = null;
Button button0;
Button button1;
Button button2;
Button button3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listener0 = new OnClickListener() {
public void onClick(View v) {
Intent intent0 = new Intent(ActivityMain.this, ActivityFrameLayout.class);
setTitle("FrameLayout");
startActivity(intent0);
}
};
listener1 = new OnClickListener() {
public void onClick(View v) {
Intent intent1 = new Intent(ActivityMain.this, ActivityRelativeLayout.class);
startActivity(intent1);
}
};
listener2 = new OnClickListener() {
public void onClick(View v) {
setTitle("这是在ActivityLayout");
Intent intent2 = new Intent(ActivityMain.this, ActivityLayout.class);
startActivity(intent2);
}
};
listener3 = new OnClickListener() {
public void onClick(View v) {
setTitle("TableLayout");
Intent intent3 = new Intent(ActivityMain.this, ActivityTableLayout.class);
startActivity(intent3);
}
};
setContentView(R.layout.main);
button0 = (Button) findViewById(R.id.button0);
button0.setOnClickListener(listener0);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(listener1);
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(listener2);
button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(listener3);
}
}
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, ActivityMain!</string>
<string name="app_name">TestMain</string>
<drawable name="red">#7f00</drawable>
<drawable name="blue">#770000ff</drawable>
<drawable name="green">#7700ff00</drawable>
<drawable name="yellow">#77ffff00</drawable>
</resources>
ActivityLayout.java
package com.android.layout;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
public class ActivityLayout extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layoutMain = new LinearLayout(this);
layoutMain.setOrientation(LinearLayout.HORIZONTAL);
setContentView(layoutMain);
LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout layoutLeft = (RelativeLayout) inflate.inflate(
R.layout.left, null);
RelativeLayout layoutRight = (RelativeLayout) inflate.inflate(
R.layout.right, null);
RelativeLayout.LayoutParams relParam = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutMain.addView(layoutLeft, 100, 100);
layoutMain.addView(layoutRight, relParam);
}
}