DevWiki

如何在 BaseActivity 中封装 Toolbar ?
1. 设置 Theme在 style.xml 文件中建立如下主题:<resources> &...
扫描右侧二维码阅读全文
23
2018/10

如何在 BaseActivity 中封装 Toolbar ?

1. 设置 Theme

在 style.xml 文件中建立如下主题:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

然后在 Android Manifest 文件中设置:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:name=".ConversationApp"
    android:theme="@style/AppTheme.NoActionBar">
    <activity
        android:name="net.devwiki.conversation.module.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>
2. 创建基类 AbstractActivity

创建抽象基类 AbstractActivity:

public abstract class AbstractActivity extends AppCompatActivity {

    public static String TAG ;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TAG = getClass().getSimpleName();
    }

    /**
     * 返回该Activity的布局文件id
     *
     * @return 布局文件id
     */
    public abstract @LayoutRes int getLayoutRes();

    /**
     * 初始化View
     */
    public abstract void initView();

    /**
     * 初始化数据
     */
    public abstract void initData();
}
3. 创建 BaseActivity 和 BaseToolbarActivity

创建不包含 Toolbar 的 BaseActivity:

public abstract class BaseActivity extends AbstractActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutRes());
    }
}

创建包含 Toolbar 的 Activity之前, 先建立布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".base.BaseToolbarActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        app:navigationIcon="@drawable/ic_arrow_back_black_24dp"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:popupTheme="@style/AppTheme.PopupOverlay"/>

    <FrameLayout
        android:id="@+id/content_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </FrameLayout>

</LinearLayout>

创建 BaseToolbarActivity:

public abstract class BaseToolbarActivity extends AbstractActivity {

    private Toolbar toolbar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_toolbar);
        toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FrameLayout contentLayout = findViewById(R.id.content_layout);
        getLayoutInflater().inflate(getLayoutRes(), contentLayout);
    }

    public Toolbar getToolbar() {
        return toolbar;
    }

}
4. 创建普通的 Activity

普通的需要 Toolbar 的 Activity 继承 BaseToolbarActivity, 不需要 Toolbar 的 Activity 继承 BaseActivity.

带 Toolbar 的普通 Activity 如下:

public class TestActivity extends BaseToolbarActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int getLayoutRes() {
        return R.layout.activity_test;
    }

    @Override
    public void initView() {
    }

    @Override
    public void initData() {
    }
}

其布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TestActivity"
        android:textSize="20sp"
        android:gravity="center"/>

</LinearLayout>

运行效果如下:

Last modification:November 10th, 2018 at 02:40 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment