本文例子用了Toolbar,需要依賴support-v7
去除Actionbar
為方便地去除Actionbar,采用了自定義theme繼承
"Theme.AppCompat.Light.NoActionBar"的方案
<!--Custom base application theme-->
<style name="CAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
AndroidManifest.xml中修改
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/CAppTheme">
Toolbar布局
include_layout_toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
style="@style/mToolbarStyle"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tb_left_tv"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:gravity="center"
android:text="" />
<TextView
android:id="@+id/tb_center_tv"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:gravity="center"
android:text="@string/app_name" />
<TextView
android:id="@+id/tb_right_tv"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:gravity="center"
android:text="" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
自定義Toolbar Style解決左邊留白的問題
通過觀察源碼雀费,發(fā)現(xiàn)是contentInsetStart在作怪,fix it肋乍!
<!--Custom Toolbar Style -->
<style name="mToolbarStyle" parent="@style/Widget.AppCompat.Toolbar">
<!-- 設(shè)置該屬性解決Toolbar左邊空白 -->
<item name="contentInsetStart">0dp</item>
</style>
BaseActivity
public class BaseActivity extends AppCompatActivity {
private static final String TAG = "BaseActivity";
/**
* Root view
*/
private LinearLayout mRootLayout;
/**
* Toolbar instance
*/
protected Toolbar mToolbar;
public Toolbar getToolbar() {
return mToolbar;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_base);
if (!isTaskRoot()) {
/* If this is not the root activity,finish it.*/
Intent intent = getIntent();
String action = intent.getAction();
if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && Intent.ACTION_MAIN.equals(action)) {
//Logger.w("Activity is not the root. Finishing Activity instead of launching.");
Log.w(TAG, "Activity is not the root. Finishing Activity instead of launching.");
finish();
return;
}
}
}
@Override
public void setContentView(int layoutId) {
setContentView(View.inflate(this, layoutId, null));
}
@Override
public void setContentView(View view) {
mRootLayout = (LinearLayout) findViewById(R.id.root_layout);
if (mRootLayout == null) {
return;
}
mRootLayout.addView(view,
new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
initToolbar();
}
/**
* 初始化Toolbar
*/
private void initToolbar() {
mToolbar = (Toolbar) findViewById(R.id.toolbar);
if (mToolbar != null) {
showToolbar(true);
//mToolbar.inflateMenu(R.menu.main);
/** 設(shè)置支持ActionBar绳慎,當(dāng)然也可以不這樣做 */
//setSupportActionBar(mToolbar);
/** 去除ActionBar默認(rèn)Title顯示 */
//getSupportActionBar().setDisplayShowTitleEnabled(false);
}
}
/**
* 顯示Toolbar
*
* @param show true:顯示,false:隱藏
*/
public void showToolbar(boolean show) {
if (mToolbar == null) {
Log.e(TAG, "Toolbar is null.");
} else {
int paddingTop = mToolbar.getPaddingTop();
int paddingBottom = mToolbar.getPaddingBottom();
int paddingLeft = mToolbar.getPaddingLeft();
int paddingRight = mToolbar.getPaddingRight();
int statusHeight = ScreenUtil.getStatusHeight(this);
ViewGroup.LayoutParams params = mToolbar.getLayoutParams();
int height = params.height;
/**
* 利用狀態(tài)欄的高度,4.4及以上版本給Toolbar設(shè)置一個(gè)paddingTop值為status_bar的高度彩匕,
* Toolbar延伸到status_bar頂部
**/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(show);
if (show) {
paddingTop += statusHeight;
height += statusHeight;
} else {
paddingTop -= statusHeight;
height -= statusHeight;
}
}
params.height = height;
mToolbar.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
mToolbar.setVisibility(show ? View.VISIBLE : View.GONE);
}
}
/**
* 設(shè)置透明狀態(tài)欄
* 對(duì)4.4及以上版本有效
*
* @param on
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
private void setTranslucentStatus(boolean on) {
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
if (on) {
winParams.flags |= bits;
} else {
winParams.flags &= ~bits;
}
win.setAttributes(winParams);
}
}
獲取狀態(tài)欄高度
/**
* 獲得狀態(tài)欄的高度
*
* @param context
* @return px
*/
public static int getStatusHeight(Context context) {
int statusHeight = -1;
try {
Class<?> clazz = Class.forName("com.android.internal.R$dimen");
Object object = clazz.newInstance();
int height = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString());
statusHeight = context.getResources().getDimensionPixelSize(height);
} catch (Exception e) {
e.printStackTrace();
}
return statusHeight;
}
BaseActivity布局
activity_base.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="@layout/include_layout_toolbar" />
</LinearLayout>
MainActivity
public class MainActivity extends BaseActivity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
default:
break;
}
}
}
MainActivity布局
activity_main.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"
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="com.elonlink.demo.MainActivity">
</RelativeLayout>