<?xml version= "1.0" encoding ="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <RelativeLayout android:id="@+id/bar" android:layout_width="match_parent" android:layout_height="80dp" android:background="#44550000" /> <RelativeLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#44005500" /> </LinearLayout>
自定義的theme,去掉ActionBar:android
<style name="AppThemeNoActionBar" parent="AppBaseTheme" > <item name= "android:windowActionBar" >false </item> <item name= "android:windowNoTitle" >true </item> </style >
package cn.carbs.testandroidbaseactivity; public class BaseActivity extends Activity{ protected RelativeLayout content; protected int colorPrimary ; protected int colorPrimaryDark ; protected int colorAccent ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(getBaseActivityLayout()); TypedArray array = obtainStyledAttributes( new int[]{R.attr.colorPrimary, R.attr. colorPrimaryDark, R.attr.colorAccent}); colorPrimary = array.getColor(0, 0xFF1473AF); colorPrimaryDark = array.getColor(1, 0xFF11659A); colorAccent = array.getColor(2, 0xFF3C69CE); array.recycle(); } protected int getBaseActivityLayout() { return R.layout.activity_base; } @Override public void setContentView(int layoutResID) { //使用以下方法能夠將layoutResID對應的 xml資源的view解析出來,並添加到R.id.content中 // getLayoutInflater().inflate(layoutResID, (ViewGroup) this.findViewById(R.id.content)); //使用以下方法能夠將layoutResID對應的 xml資源的view解析出來,並添加到R.id.content中 // View.inflate(this, layoutResID, (ViewGroup) this.findViewById(R.id.content)); //使用以下方法能夠將layoutResID對應的 xml資源的viewinflate出來,可是沒有添加到任何ViewGroup中 // View v = View.inflate(this, layoutResID, null); setContentView(View. inflate(this, layoutResID, null)); } @Override public void setContentView(View view) { ((ViewGroup) this.findViewById(R.id.content)) .addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams. MATCH_PARENT)); } }
3.編寫一個Activity使其繼承自BaseActivity:app
package cn.carbs.testandroidbaseactivity; import android.os.Bundle; public class MainActivity extends BaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout. activity_main); } //能夠經過以下代碼,覆寫BaseActivity中的主界面的佈局 @Override protected int getBaseActivityLayout(){ return R.layout. activity_base_new; } }
<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" android:background= "#33333399" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop= "@dimen/activity_vertical_margin" tools:context= ".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </RelativeLayout>
activity_base.xml框架
<?xml version= "1.0" encoding ="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <RelativeLayout android:id="@+id/bar" android:layout_width="match_parent" android:layout_height="80dp" android:background="#44550000" /> <RelativeLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#44005500" /> </LinearLayout>
activity_base_new.xmlide
<?xml version= "1.0" encoding ="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <RelativeLayout android:id="@+id/bar" android:layout_width="match_parent" android:layout_height="80dp" android:background="#ffffffff" /> <RelativeLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#44005500" /> </LinearLayout>