如果一个项目有多个界面有一部分组件是共用的话,我们可以通过在父类中用一个布局把共用的组件写在一个布局文件中。
例: base_view.xml 父布局,将相同的布局放在父布局,也预留不同部分的布局来重写
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<!-- 共用标题的组件 -->
<TextView
android:id="@+id/title_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<!-- 共用底部的组件 -->
<TextView
android:id="@+id/bottom_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="@string/bottom_text" />
<!-- 中间不同的部分 -->
<LinearLayout
android:id="@+id/middle_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/bottom_text"
android:layout_below="@id/title_text"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
在父类中可以重写 onCreate()方法
package com.xwtec.demo;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
public class BaseActivity extends Activity
{
private LinearLayout middleLayout;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.setContentView(R.layout.base_view);
middleLayout = (LinearLayout)findViewById(R.id.middle_layout);
}
public void setContentView(int layoutId)
{
View middleView = getLayoutInflater().inflate(layoutId, null);
if (null != middleLayout)
{
middleLayout.removeAllViews();
middleLayout.addView(middleView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
}
}
}
子类的布局:view.xml 不同部分的布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/title_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
子类MainActivity.java类
package com.xwtec.demo;
import android.os.Bundle;
//继承父类
public class MainActivity extends BaseActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.setContentView(R.layout.view);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xwtec.demo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.xwtec.demo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>