android中的LayoutInflater简单运用

.在做项目的工程中,经常会用到一些相同的背景框,但是前景的选择不同。这个时候可以使用LayoutInflater来实现。

代码如下:

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent" android:id="@+id/main_layout">
	<TextView android:layout_width="fill_parent" android:gravity="center"
		android:layout_height="wrap_content" android:text="@string/info" />
	<Button android:id="@+id/button1" android:text="@string/dialog1"
		android:layout_width="match_parent" android:layout_height="wrap_content"></Button>
	<Button android:id="@+id/button2" android:text="@string/dialog2"
		android:layout_width="match_parent" android:layout_height="wrap_content"></Button>
</LinearLayout>
inflater.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/relativeLayout1"
	android:layout_width="fill_parent" android:layout_height="fill_parent"
	xmlns:android="http://schemas.android.com/apk/res/android">
	<Button android:text="@string/personal_cancel"
		android:layout_width="wrap_content" android:id="@+id/button2"
		android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button1"
		android:layout_alignBottom="@+id/button1"
		android:layout_alignParentRight="true" android:layout_marginRight="70dp"></Button>
	<EditText android:layout_width="100dp" android:id="@+id/editText1"
		android:inputType="textPersonName" android:layout_height="wrap_content"
		android:layout_alignParentTop="true" android:layout_centerHorizontal="true">
		<requestFocus></requestFocus>
	</EditText>
	<TextView android:layout_width="wrap_content" android:id="@+id/textView1"
		android:text="@string/inputName" android:layout_height="wrap_content"
		android:layout_alignBaseline="@+id/editText1"
		android:layout_alignBottom="@+id/editText1"
		android:layout_alignParentLeft="true"
		android:textColor="#ffffff"></TextView>
	<Button android:text="@string/personal_save"
		android:layout_width="wrap_content" android:id="@+id/button1"
		android:layout_height="wrap_content" android:layout_below="@+id/editText1"
		android:layout_toLeftOf="@+id/editText1"></Button>

</RelativeLayout>


inflater2,xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/rank_layout"
	android:layout_width="fill_parent" android:layout_height="fill_parent"
	xmlns:android="http://schemas.android.com/apk/res/android">
	<TextView android:layout_width="wrap_content" android:id="@+id/textView1"
		android:textAppearance="?android:attr/textAppearanceLarge"
		android:text="@string/second_inflater" android:layout_height="wrap_content"
		android:layout_alignParentTop="true" android:layout_centerHorizontal="true"
		android:layout_marginTop="35dp"></TextView>
</RelativeLayout>


background.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="300dp"
  android:layout_height="150dp"
  android:background="@drawable/bg"
  android:id="@+id/bglayout">
    
</LinearLayout>

java代码部分:

InflaterActivity,java

package fover.inflater;

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 InflaterActivity extends Activity implements OnClickListener {

	private Button button1;
	private Button button2;

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

	}

	/**
	 * 描述:设置监听事件
	 */
	private void setOnclick() {
		button1.setOnClickListener(this);
		button2.setOnClickListener(this);
	}

	/**
	 * 描述:发现各种VIEW
	 */
	private void findView() {
		button1 = (Button) findViewById(R.id.button1);
		button2 = (Button) findViewById(R.id.button2);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button1:
			Intent intent=new Intent(InflaterActivity.this, personal.class);
			startActivity(intent);
			break;
		case R.id.button2:
			Intent intent2=new Intent(InflaterActivity.this, rank.class);
			startActivity(intent2);
			break;

		default:
			break;
		}
	}
}
personal.java

package fover.inflater;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.LinearLayout;

public class personal extends Activity {

	private View inflaterView;
	private LinearLayout bgView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.background);
		
		bgView=(LinearLayout)findViewById(R.id.bglayout);
		
		LayoutInflater inflater=LayoutInflater.from(this);
		inflaterView=inflater.inflate(R.layout.inflater, null);
		bgView.addView(inflaterView,new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
		
	}

}


rank.java

package fover.inflater;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.LinearLayout;


public class rank extends Activity {

	private LinearLayout bigView;
	private View inflaterView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.background);
		bigView=(LinearLayout)findViewById(R.id.bglayout);
		
		LayoutInflater inflater=LayoutInflater.from(this);
		inflaterView=inflater.inflate(R.layout.inflater2, null);
		
		
		bigView.addView(inflaterView,new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
		
		
		
	}

}


需要注意的是需要在:AndroidManifest.xml设置权限哦。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="fover.inflater"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".InflaterActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="personal" android:theme="@android:style/Theme.Dialog" ></activity>
        <activity android:name="rank" android:theme="@android:style/Theme.Dialog"></activity>

    </application>
</manifest>
运行效果截图:



posted @ 2012-08-13 13:05  爱生活,爱编程  阅读(1249)  评论(0编辑  收藏  举报