如何在多个活动中保持浮动操作按钮一致?

时间:2016-10-28 11:41:26

标签: android performance

我正在构建一个具有浮动操作按钮的应用程序,可以打开菜单。我希望能够保持菜单的一致性,而不必将其添加到每个活动中。

我认为我可以简单地创建MainActivity并将一个ViewStub放在一个具有id" holder"的LinearLayout中。然后,当单击菜单按钮时,我只是从持有者中删除视图并添加新视图。

这种方法有什么缺陷吗?有关更好方法的任何建议吗?

这是我的主要活动:

public class MainActivity extends AppCompatActivity {

    private LinearLayout holder;
    private ViewStub viewStub;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        holder = (LinearLayout) findViewById(R.id.holder);

        viewStub = (ViewStub) findViewById(R.id.viewStub);
        viewStub.setLayoutResource(R.layout.content_main);
        viewStub.inflate();

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        switch(id){
            case R.id.action_main:
                loadLayout(R.layout.content_main);
                break;
            case R.id.action_secondary:
                loadLayout(R.layout.content_secondary);
                break;
        }

        return super.onOptionsItemSelected(item);
    }

    private void loadLayout(int layout){
        holder.removeAllViews();
        holder.addView(LayoutInflater.from(this).inflate(layout,holder,false));
    }
}

这是我的主要布局,我将内容布局加载到" holder":

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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:fitsSystemWindows="true"
    tools:context="com.example.android.simplepopoutmenu.MainActivity">

    <LinearLayout
        android:id="@+id/holder"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ViewStub
            android:id="@+id/viewStub"
            android:inflatedId="@+id/main_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|start"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_menu_view" />

</android.support.design.widget.CoordinatorLayout>

0 个答案:

没有答案