繁体   English   中英

不要将工具栏连接到导航抽屉怎么办?

[英]Do not connect toolbar to navigation drawer how to do it?

我使用MaterialDrawer库,但是由于某些未知原因,Drawer本身可以工作,而工具栏却不能。

我有一个从中扩展活动的类,它包含以下方法:

public void  Drawer(Toolbar toolbar, Context context, android.app.Activity activity){
    setSupportActionBar(toolbar);

    DatabaseReference rootRef2 = FirebaseDatabase.getInstance().getReference();
    DatabaseReference uidRef2 = rootRef2.child("users").child(getUid());
    ValueEventListener valueEventListener2 = new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            String email = dataSnapshot.child("account").getValue(String.class);
            String Avatar = dataSnapshot.child("Avatar").getValue(String.class);
            Long coinsAmount = dataSnapshot.child("coinsAmount").getValue(Long.class);

            String coinsView = "Баланс: " + coinsAmount;




                if (Avatar.equals("1")) {
                    avatar = (R.drawable.a1);
                }
                if (Avatar.equals("2")) {
                    avatar = (R.drawable.a2);
                }


            IProfile profile = new ProfileDrawerItem()
                    .withName(email)
                    .withEmail(coinsView)
                    .withIcon(avatar);

            AccountHeader headerResult = new AccountHeaderBuilder()
                    .withActivity(activity)
                    .withHeaderBackground(R.color.grey_300)
                    .addProfiles(
                            profile
                    )
                    .build();


               result = new DrawerBuilder()
                    .withActivity(activity)
                    .withToolbar(toolbar)
                    .withAccountHeader(headerResult)
                    .addDrawerItems(
                            new PrimaryDrawerItem()
                                    .withName("Устройства")
                                    .withIcon(R.drawable.device)
                                    .withIdentifier(1)
                    )
                    .addDrawerItems(
                            new PrimaryDrawerItem()
                                    .withName("Сканер")
                                    .withIcon(R.drawable.scaner)
                                    .withIdentifier(2)
                    )
                    .addDrawerItems(
                            new PrimaryDrawerItem()
                                    .withName("Магазины")
                                    .withIcon(R.drawable.shop)
                                    .withIdentifier(3)
                    )
                    .addDrawerItems(
                            new PrimaryDrawerItem()
                                    .withName("Купоны")
                                    .withIcon(R.drawable.coupons)
                                    .withIdentifier(4)
                    )
                    .addDrawerItems(
                            new PrimaryDrawerItem()
                                    .withName("Архив Купонов")
                                    .withIcon(R.drawable.oldcoupons)
                                    .withIdentifier(5)
                    )
                    .withOnDrawerItemClickListener((view, i, iDrawerItem) -> {
                        if(iDrawerItem.getIdentifier() == 1 && !Activity.equals("Menu")){
                            Intent intent = new Intent(context,MenuActivity.class);
                            startActivity(intent);
                        }
                        else if(iDrawerItem.getIdentifier() == 2 && !Activity.equals("Scaner")){
                            Intent intent = new Intent(context,DecoderActivity.class);
                            startActivity(intent);
                        }
                        else if(iDrawerItem.getIdentifier() == 3 && !Activity.equals("Shop")){
                            Intent intent = new Intent(context,ShopActivity.class);
                            startActivity(intent);
                        }
                        else if(iDrawerItem.getIdentifier() == 4 && !Activity.equals("oldCoupons")){
                            Intent intent = new Intent(context,oldCouponsActivity.class);
                            startActivity(intent);
                        }

                        return false;
                    })
                    .build();

            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setHomeButtonEnabled(false);

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }

    };
    uidRef2.addValueEventListener(valueEventListener2);




}

所以我连接了抽屉

Drawer(toolbar,LibraryActivity.this,LibraryActivity.this);

XML此活动:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout            xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>


<include
    android:id="@+id/mytoolbar"
    layout="@layout/toolbar" />
<include layout="@layout/content_menu" />

</androidx.constraintlayout.widget.ConstraintLayout>

另一个重要的细节是,如果将setSupportActionBarsetSupportActionBar工具栏的启动一起放置在OnCreate Activity中,则会在第二个加载并且立即消失

这是实际的样子

GIF

Toolbar.xml

<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:iosched="http://schemas.android.com/apk/res-auto"


android:id="@+id/toolbar_actionbar"
android:layout_width="match_parent"
android:layout_height="@dimen/materialize_toolbar"
android:background="@color/grey_500"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme" />

Map.xml

<?xml version="1.0" encoding="utf-8"?>
 <fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.Activities.MenuActivity"
/>

尝试像这样构建布局:

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

        <LinearLayout
            android:id="@+id/container_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <!-- Include your Toolbar -->
            <include
                android:id="@+id/mytoolbar"
                layout="@layout/toolbar" />
        </LinearLayout>

        <FrameLayout
            android:id="@+id/container_body"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
            <!-- Embed your fragment in a Layout View-->
            <fragment xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:map="http://schemas.android.com/apk/res-auto"
                xmlns:tools="http://schemas.android.com/tools"
                android:id="@+id/map"
                android:name="com.google.android.gms.maps.SupportMapFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context="com.example.Activities.MenuActivity"
                />
        </FrameLayout>

</LinearLayout>

可以优化此布局,但是请尝试使用上面的示例。 基本上,我们需要创建两行:用于维护工具栏的一行和用于包装地图视图容器的一行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM