【问题标题】:Add Navigation Drawer fragment to activity将导航抽屉片段添加到活动
【发布时间】:2016-10-14 16:14:48
【问题描述】:

我想将我的导航抽屉片段添加到活动中,但我希望导航抽屉“后面”的活动布局保持不变。

我得到了什么

未打开导航抽屉

我想要什么

导航抽屉片段活动 XML

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <RelativeLayout
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:id="@+id/drawerPane"
        android:layout_gravity="start">


        <RelativeLayout
            android:id="@+id/logoBox"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="@color/material_blue_grey_800"
            android:padding="8dp" >

            <ImageView
                android:id="@+id/g4aLogo"
                 android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_marginTop="15dp"
                android:layout_centerInParent="true"/>

        </RelativeLayout>

        <ListView
            android:id="@+id/settingsList"
            android:layout_width="280dp"
            android:layout_height="match_parent"
            android:layout_below="@+id/logoBox"
            android:choiceMode="singleChoice"
            android:background="#ffffffff" />
    </RelativeLayout>
</android.support.v4.widget.DrawerLayout>

活动 XML

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/activity_main"
    android:orientation="vertical"
    android:weightSum="10">


    <LinearLayout
        android:layout_weight="1"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/searchBarLay"
        android:focusable="true"
        android:focusableInTouchMode="true">


//CONTENTS and layouts...


</LinearLayout>
</LinearLayout>

实现导航抽屉时的活动代码部分

private void initSettingsDrawer() {
        Button settingsDrawerButton =(Button)findViewById(R.id.settingsDrawer);
        settingsDrawerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SettingsDrawer settingsDrawer = new SettingsDrawer();
                FragmentManager fragmentManager = getSupportFragmentManager();
                fragmentManager.beginTransaction().add(R.id.activity_main, settingsDrawer).commit();
            }
        });
    }

【问题讨论】:

    标签: android android-fragments navigation-drawer


    【解决方案1】:

    android.support.v4.widget.DrawerLayout 应该是父/根布局。你的xml应该是这样的,

    <android.support.v4.widget.DrawerLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/drawerLayout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      >
    
    
    <RelativeLayout
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:id="@+id/drawerPane"
        android:layout_gravity="start">
    
    
        <RelativeLayout
            android:id="@+id/logoBox"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="@color/material_blue_grey_800"
            android:padding="8dp" >
    
            <ImageView
                android:id="@+id/g4aLogo"
                 android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_marginTop="15dp"
                android:layout_centerInParent="true"/>
    
        </RelativeLayout>
    
        <ListView
            android:id="@+id/settingsList"
            android:layout_width="280dp"
            android:layout_height="match_parent"
            android:layout_below="@+id/logoBox"
            android:choiceMode="singleChoice"
            android:background="#ffffffff" />
    </RelativeLayout>
    </android.support.v4.widget.DrawerLayout>
    

    【讨论】:

    • 当我从 xml 中删除相对布局时出现错误:DrawerLayout 必须使用 MeasureSpec.EXACTLY 错误进行测量。我应该创建自定义 DrawerLayout 然后它会起作用吗?
    • 我做了自定义并摆脱了错误,但删除相对布局表单 DrawerLayout XML 并不能解决我的问题(我仍然在导航抽屉后面有空格
    • 更新有和没有导航抽屉的屏幕截图。
    • 在你的片段中添加这一行 "drawerLayout.setScrimColor(getResources().getColor(android.R.color.transparent));"
    • 还是什么都没有。我还尝试将透明背景设置为 XML 文件
    【解决方案2】:

    将此库添加到您的 build.gradle 文件中

    编译('com.mikepenz:materialdrawer:5.1.3@aar') { 传递=真}

    然后将此代码添加到您想要导航抽屉的 Activity 中。

    public class MainActivity extends AppCompatActivity {
    private Drawer result = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
       Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    
    
        AccountHeader headerResult = new AccountHeaderBuilder()
                .withActivity(this)
               // .withHeaderBackground(R.drawable.nav_header)
    
                .addProfiles(
                        new ProfileDrawerItem().withEmail("abc@gmail.com").withIcon(getResources().
                                getDrawable(R.mipmap.ic_launcher))
                )
                .withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
                    @Override
                    public boolean onProfileChanged(View view, IProfile profile, boolean currentProfile) {
                        return false;
                    }
                })
                .build();
    
        result = new DrawerBuilder()
                .withActivity(this)
                .withToolbar(toolbar)
                .withActionBarDrawerToggleAnimated(true)
                .withAccountHeader(headerResult)
                .build();
        result.addItem(new PrimaryDrawerItem()
                .withName(("Home")).withIcon(R.mipmap.ic_launcher).
                        withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                            @Override
                            public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                                return false;
                            }
                        }));
        result.addItem(new PrimaryDrawerItem()
                .withName(("Settings")).withIcon(R.mipmap.ic_launcher).
                        withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                            @Override
                            public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                                return false;
                            }
                        }));
    
    }
    

    }

    【讨论】:

    • 我宁愿使用不使用外部库的解决方案,而且我想将我的导航驱动程序实现到几个独立的活动中,我不想在每个活动中重复代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多