Android工具栏更改布局 [英] Android toolbar change layout

查看:52
本文介绍了Android工具栏更改布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多碎片的活动.当我更改片段时,我也需要更改,并且我的工具栏在 MainActivity.class 中声明.我的问题是我包括一个布局,但没有找到更改此布局的方法.在这种情况下如何更改工具栏的布局? MainActivity.class

I got an activity with many fragments. When i change fragments i need to change also and my toolbar declared in MainActivity.class. My problem is that i include a layout and didnt find a method to change this layout. How to change toolbars layout in this case? 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>

推荐答案

如果我理解正确,那么只要更改用户当前正在查看的Fragment,就想更改工具栏,不是吗?

If I understood right, you want to change the toolbar whenever you change the Fragment the user is currently seeing, isn´t that right?

有一种非常简单的方法来实现XML.假设您要使用三种不同样式的工具栏.

There is a very simple way to achieve this trough your XML. Let's say you want three different styled toolbars.

1)首先,为所需的每个工具栏创建XML文件.

1) First, you create XML files for every toolbar you need.

2)然后,将每个工具栏都包含在 activity_main.xml 中,如下所示:

2) Then you include every toolbar in to your activity_main.xml, like this:

<?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>

看看所有三个工具栏都具有可见性属性吗?

See how all three toolbar includes have the visibility property?

好吧,现在您可以使用此属性进行设置,并在需要时/需要时显示/隐藏所需的工具栏.

Well, now you can toy with this property, and show/hide the desired toolbar whenever you want/need to.

例如:

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);

然后根据您的需要更改要显示的是哪一个.

And just change which one are you making visible, according to your needs.

希望这会有所帮助.

这篇关于Android工具栏更改布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆