安卓工具栏更改布局

6

我有一个包含多个片段的活动。当我改变片段时,我需要同时改变在MainActivity.class中声明的工具栏。我的问题是,我包含了一个布局,并没有找到改变这个布局的方法。在这种情况下如何改变工具栏的布局呢?

 @ContentView(R.layout.activity_main)
    public class MainActivity extends RoboActionBarActivity {

        @InjectView(R.id.tool_bar)
        Toolbar toolbar;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setToolBar();
            if (savedInstanceState == null) {
                getSupportFragmentManager().beginTransaction()
                        .add(R.id.container, new SplashFragment())
                        .commit();
            } else {
                getSupportFragmentManager().beginTransaction()
                        .replace(R.id.container, new HomeFragment())
                        .commit();
            }
        }
    private void setToolBar() {
            setSupportActionBar(toolbar);
        }

        @Override
        public void onAttachFragment(Fragment fragment) {
            super.onAttachFragment(fragment);
            String currentFragmentClass = fragment.getClass().getSimpleName();
            if (currentFragmentClass.equals(getString(R.string.info_fragment))) {
    //here i need to change and set onclicks
            }
        }
    }

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
    <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"
        tools:context=".activities.MainActivity">

        <include
            android:id="@+id/tool_bar"
            layout="@layout/tool_bar" />

        <View
            android:id="@+id/shadow_view"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_below="@+id/tool_bar"
            android:background="#ad8c22" />

        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/shadow_view"
            tools:context=".activities.MainActivity"
            tools:ignore="MergeRootFrame" />
    </RelativeLayout>

tool_bar.xml

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorToolBar"
    >
    <RelativeLayout
        android:id="@+id/toolbar_main_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="5dp">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:onClick="toggleSlidingMenu"
            android:src="@mipmap/icon_sliding_menu" />
        <ImageView
            android:id="@+id/app_logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:src="@mipmap/jammboree" />
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/app_logo"
            android:padding="5dp">
            <ImageView
                android:id="@+id/hotlist_bell"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:contentDescription="bell"
                android:gravity="center"
                android:padding="7dp"
                android:src="@mipmap/icon_my_cart" />
            <TextView
                android:id="@+id/hotlist_hot"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignRight="@id/hotlist_bell"
                android:layout_alignTop="@id/hotlist_bell"
                android:background="@drawable/rounded_square"
                android:gravity="center"
                android:minWidth="17sp"
                android:padding="2dp"
                android:text="2"
                android:textColor="#ffffffff"
                android:textSize="12sp" />
            <TextView
                android:id="@+id/myMoneyInMyPocket"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_below="@+id/hotlist_bell"
                android:text="2000$"
                android:textColor="#ffffff"
                android:textSize="14sp"
                android:textStyle="bold" />
        </RelativeLayout>
    </RelativeLayout>
</android.support.v7.widget.Toolbar>

我的类似问题的答案得到了赞同。 http://stackoverflow.com/questions/35303630/how-to-update-toolbar-options-from-fragment/35327518#35327518。 这有帮助吗? - BryanT
2个回答

9
如果我理解正确,您想在用户当前查看的Fragment更改时更改工具栏,是吗?
通过您的XML可以非常简单地实现这一点。假设您想要三个不同样式的工具栏。
1) 首先,为您需要的每个工具栏创建XML文件。
2) 然后将每个工具栏包含到您的activity_main.xml中,就像这样:
<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".activities.MainActivity">

    <include
        android:id="@+id/toolbar1"
        layout="@layout/tool_bar1"
        android:visibility="visible" />

    <include
        android:id="@+id/toolbar2"
        layout="@layout/tool_bar2"
        android:visibility="gone" />

    <include
        android:id="@+id/toolbar3"
        layout="@layout/tool_bar3"
        android:visibility="gone" />

    // Here is the rest of your XML code

</RelativeLayout>

看到所有三个工具栏都包含了visibility属性了吗?

现在,你可以玩弄这个属性,随时显示/隐藏所需的工具栏。

例如:

RelativeLayout mLayout = (RelativeLayout) getActivity().findViewById(R.id.my_main_layout);

Toolbar mToolbar1 = (Toolbar) mLayout.findViewById(R.id.toolbar1);
mToolbar1.setVisibility(View.GONE);

Toolbar mToolbar2 = (Toolbar) mLayout.findViewById(R.id.toolbar2);
mToolbar2.setVisibility(View.GONE);

Toolbar mToolbar3 = (Toolbar) mLayout.findViewById(R.id.toolbar3);
mToolbar3.setVisibility(View.VISIBLE);

只需根据需要更改可见的内容即可。

希望这能有所帮助。


即使可见性为GONE,每个工具栏不都会占用内存空间吗?当您拥有一个有20个不同工具栏的大型应用程序时会发生什么? - Alex Busuioc
@AlexBusuioc 我自己没有尝试过,但是重绘视图并使布局重新计算其绘制位置对性能来说并不特别好。一个.GONE视图可以节省这一部分,我猜你可以测量哪个版本更适合你的用例。 - herrmartell

1

我认为@herrmartell的方法不太好;

我的方法是,在一个Activity中有三个Fragment,我为共同的工具栏xml定义了一些不同的布局资源,并在单击不同的按钮时加载它们到工具栏中;我的代码:

  1. common_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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar">
    
    <!-- your custom layout -->
    
    </android.support.v7.widget.Toolbar>
    
  2. activity_main_toolbar_common_view.xml

    <TextView
        android:id="@+id/action_location"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="迪士尼...ˇ"
        android:textColor="@color/white"
        android:textSize="@dimen/larget_text_size"
        android:paddingLeft="10dp"
        android:layout_alignParentStart="true"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"/>
    

  3. activity_main_toolbar_mine_view.xml

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/mine"
        android:textSize="@dimen/big_text_size"
        android:textColor="@color/white"
        android:gravity="center"
        android:layout_centerInParent="true"/>
    
    <ImageView
        android:id="@+id/setting"
        android:layout_width="@dimen/toolbar_icon_size"
        android:layout_height="@dimen/toolbar_icon_size"
        android:src="@drawable/toolbar_setting"
        android:layout_alignParentRight="true"
       />
    

  4. switchToolbar() method in Activity:

    public void switchToolbar(int layout) {
        if(currentToolbarLayout == layout){
            return;
        }
        currentToolbarLayout = layout;
        View  v = getLayoutInflater().inflate(layout,null);
        toolbar.removeAllViews();
        toolbar.addView(v);
    
        switch (layout){
            case R.layout.activity_main_toolbar_common_view:
                v.findViewById(R.id.action_location).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MainActivity.this, "跳到位置", Toast.LENGTH_SHORT).show();
                    }
                });
                break;
            case R.layout.activity_main_toolbar_mine_view:
                v.findViewById(R.id.setting).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MainActivity.this, "跳到设置", Toast.LENGTH_SHORT).show();
                    }
                });
                break;
        }
    }
    
  5. I render my toolbar view in the onCreate() method of Activity:

    ButterKnife.bind(this);
    setSupportActionBar(toolbar);
    switchToolbar(R.layout.activity_main_toolbar_common_view);
    
  6. when I switch toolbar, I call:

    switchToolbar(R.layout.activity_main_toolbar_mine_view);
    

1
这样做没问题,但需要更多的编写和维护工作,而且并没有真正提供性能优势。此外,你的解决方案并没有真正解决工具栏属性的变化,例如背景和前景颜色、高度等。记住,你应该尽可能地保持代码简单和清晰。 - herrmartell
说实话,我更喜欢这个;随着时间的推移,MainActivity中的片段将会发生变化。我认为把所有特定的工具栏都放在MainActivity中是不可维护的。尤其是当自定义工具栏很复杂时。 - Kenn Cal
1
补充@herrmartell的评论。在工具栏中保留视图的最后一个实例非常容易,而且没有太多开销。删除所有视图然后重新附加视图会使您失去视图的实例,您需要重新创建它。因此,对于我的用例场景,我选择跟随herrmartell的建议。 - Ankit Gupta

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接