由于明天面试,就直接看源码吧,祝自己好运^_^~
先上效果图
BaseActivity
package com.example.android_004;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class BaseActivity extends Activity implements OnClickListener {
private LinearLayout parentRoot;
private Button btnBack;
private Button btnEdit;
private TextView tvShow;
private LinearLayout llRoot;
public LinearLayout getParentRoot() {
return parentRoot;
}
public Button getBtnBack() {
return btnBack;
}
public Button getBtnEdit() {
return btnEdit;
}
public TextView getTvShow() {
return tvShow;
}
public LinearLayout getLlRoot() {
return llRoot;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
initComp(R.layout.activity_base);
}
private void initComp(int activityBase) {
// TODO Auto-generated method stub
ViewGroup group = (ViewGroup) findViewById(android.R.id.content);
parentRoot = new LinearLayout(this);
parentRoot.setOrientation(LinearLayout.VERTICAL);
group.addView(parentRoot);
View view = LayoutInflater.from(this).inflate(activityBase, parentRoot,
true);
btnBack = (Button) view.findViewById(R.id.btn_title_back);
btnEdit = (Button) view.findViewById(R.id.btn_title_edit);
btnBack.setOnClickListener(this);
btnBack.setOnClickListener(this);
tvShow = (TextView) view.findViewById(R.id.tv_title_show);
llRoot = (LinearLayout) view.findViewById(R.id.root);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_title_back:
finish();
break;
case R.id.btn_title_edit:
ToastUtils.toastShort("edit");
break;
default:
break;
}
}
@Override
public void setContentView(int layoutResID) {
// TODO Auto-generated method stub
LayoutInflater.from(this).inflate(layoutResID, parentRoot, true);
}
@Override
public void setContentView(View view) {
// TODO Auto-generated method stub
parentRoot.addView(view);
}
@Override
public void setContentView(View view, LayoutParams params) {
// TODO Auto-generated method stub
parentRoot.addView(view, params);
}
}
activity_base.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#660000ff"
>
<Button
android:id="@+id/btn_title_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="后退"
android:textColor="#fff"
android:layout_gravity="center"
android:layout_margin="5dp"
/>
<TextView
android:id="@+id/tv_title_show"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="this is a textView "
android:layout_gravity="center"
android:textSize="20sp"
android:gravity="center"
android:textColor="#fff"
/>
<Button
android:id="@+id/btn_title_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="编辑"
android:textColor="#fff"
android:layout_gravity="center"
android:layout_margin="5dp"
/>
</LinearLayout>
MainActivity
package com.example.android_004;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
public class MainActivity extends BaseActivity implements OnClickListener {
private Button btnUpdateText;
private Button btnUpdateGroud;
private Button btnJump;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
// TODO Auto-generated method stub
btnUpdateGroud = (Button) findViewById(R.id.btn_update_backgroud);
btnUpdateText = (Button) findViewById(R.id.btn_update_text);
btnJump = (Button) findViewById(R.id.btn_jump);
btnUpdateText.setOnClickListener(this);
btnUpdateGroud.setOnClickListener(this);
btnJump.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
super.onClick(v);
switch (v.getId()) {
case R.id.btn_update_text:
getTvShow().setText("text title");
break;
case R.id.btn_update_backgroud:
getLlRoot().setBackgroundColor(Color.RED);
break;
case R.id.btn_jump:
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
break;
default:
break;
}
}
}
activity_main
<LinearLayout 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: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"
android:orientation="vertical">
<Button
android:id="@+id/btn_update_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改text" />
<Button
android:id="@+id/btn_update_backgroud"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改backgroud" />
<Button
android:id="@+id/btn_jump"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转" />
</LinearLayout>
MyApplication
package com.example.android_004;
import android.app.Application;
import android.content.Context;
public class MyApplication extends Application {
private static Context mcContext;
public static Context getContext(){
return mcContext;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
mcContext = getApplicationContext();
}
}
SecondActivity
package com.example.android_004;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
public class SecondActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
ininData();
}
private void ininData() {
// TODO Auto-generated method stub
getTvShow().setText("second Activity");
getLlRoot().setBackgroundColor(Color.YELLOW);
getBtnEdit().setVisibility(View.GONE);
}
public void click(View view){
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}
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" >
<Button
android:onClick="click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="back" />
</LinearLayout>
ToastUtils
package com.example.android_004;
import android.widget.Toast;
public class ToastUtils {
public static void toastShort(String content){
Toast.makeText(MyApplication.getContext(), content, Toast.LENGTH_SHORT).show();
}
public static void toastLong(String content){
Toast.makeText(MyApplication.getContext(), content, Toast.LENGTH_LONG).show();
}
}
资源清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android_004"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:name="com.example.android_004.MyApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.android_004.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>
<activity
android:label="second"
android:name="com.example.android_004.SecondActivity"></activity>
</application>
</manifest>