简体   繁体   English

导航抽屉Android上的底部布局

[英]Bottom layout on a navigation drawer Android

Trying to add a footer to a navigation drawer that consists of two lists. 尝试将页脚添加到包含两个列表的导航抽屉中。 I am not doing this right, I understood that anything that i want in the navigation drawer should be encapsulated in the 我没有这样做,我明白我想要的任何东西都应该封装在导航抽屉里

<LinearLayout
            android:id="@+id/left_drawer_layout"

I would like to add a bottom layout that does not depend on ListView scroll. 我想添加一个不依赖于ListView滚动的底部布局。 I tried different versions of where to add the bottom layout code but nothing happens. 我尝试了不同版本的地方添加底部布局代码但没有任何反应。 I need an extra eye, please. 我需要额外的眼睛。 Thank you. 谢谢。

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">

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



        <!-- Framelayout to display Fragments -->

        <FrameLayout
            android:id="@+id/frame_container"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

    </LinearLayout>


    <!-- Listview to display slider menu -->

    <LinearLayout
        android:id="@+id/left_drawer_layout"
        android:layout_width="@dimen/navigation_drawer_max_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:orientation="vertical">


        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:background="@color/list_background">

            <Button
                android:id="@+id/refreshBtn"
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:text="@string/refresh"
                android:visibility="gone" />

            <EditText
                android:id="@+id/searchMenuTxt"
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:background="@color/list_background_pressed"
                android:drawableLeft="@drawable/search_web"
                android:drawablePadding="@dimen/activity_horizontal_margin"
                android:drawableStart="@drawable/search_web"
                android:focusable="false"
                android:focusableInTouchMode="true"
                android:hint="@string/search"
                android:paddingLeft="8dp"
                android:singleLine="true"
                android:textColorHint="@android:color/darker_gray"
                android:textSize="@dimen/text_size_14"></EditText>

            <Button
                android:id="@+id/clearBtn"
                android:layout_width="24dp"
                android:layout_height="24dp"
                android:layout_gravity="right|center"
                android:background="@drawable/mob_clear"
                android:paddingRight="8dp"
                android:visibility="invisible" />

        </FrameLayout>

        <LinearLayout
            android:id="@+id/left_drawer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <ListView
                android:id="@+id/activeChatsList"
                android:layout_width="@dimen/navigation_drawer_max_width"
                android:layout_height="wrap_content"
                android:layout_gravity="start"
                android:background="@color/list_background_pressed"
                android:choiceMode="singleChoice"
                android:divider="@drawable/list_divider"
                android:dividerHeight="1dp"
                android:fadeScrollbars="false"
                android:fastScrollEnabled="false" />

            <ListView
                android:id="@+id/drawerListView"
                android:layout_width="@dimen/navigation_drawer_max_width"
                android:layout_height="match_parent"
                android:layout_gravity="start"
                android:background="@drawable/list_selector"
                android:choiceMode="singleChoice"
                android:divider="@drawable/list_divider"
                android:dividerHeight="1dp"
                android:drawSelectorOnTop="true"
                android:fastScrollEnabled="false"
                android:minHeight="250dp" />
            <LinearLayout
                android:layout_width="fill_parent" android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_alignParentBottom="true">

                <Button android:id="@+id/CancelButton" android:layout_width="wrap_content"
                    android:layout_height="wrap_content" android:text=“Profile" />
                >

            </LinearLayout>
        </LinearLayout>


    </LinearLayout>

</android.support.v4.widget.DrawerLayout>

You can have the basic idea from this code snippet. 您可以从此代码段中获得基本想法。 And android now has the built-in functionality for menu drawer. Android现在具有菜单抽屉的内置功能。 What you are trying to do is old approach. 你要做的是老方法。 Actionbar sherlock is deprecated. 不推荐使用Actionbar sherlock。 So i would recommend you to move to AppCompat. 所以我建议你转到AppCompat。 With Appcompat following is the code for having the layout of menu drawer along with the footer. 使用Appcompat以下是将菜单抽屉与页脚一起布局的代码。

drawer_fragment.xml drawer_fragment.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res/com.focial"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white" >

    <LinearLayout
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:clickable="true"
        android:background="@android:color/black"
        android:orientation="vertical">

     <!-- any addition stuff you want in yoour footer layout -->

   </LinearLayout>

    <ListView
        android:id="@+id/drawer_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/footer"
        android:choiceMode="singleChoice"
        android:dividerHeight="1dp" />
</RelativeLayout>

activity_home.xml (where you want to enable/show drawer) activity_home.xml(您要启用/显示抽屉的位置)

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white" >

<FrameLayout
    android:id="@+id/realcontent"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<fragment
    android:id="@+id/navigation_drawer"
    android:name="com.focial.fragment.NavigationDrawerFragment"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start" />

HomeActivity.java HomeActivity.java

private NavigationDrawerFragment mNavigationDrawerFragment;
private ArrayList<DrawerItem> drawerItems;
private DrawerLayout drawerLayout;

protected void initUIComponents() {
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
    mNavigationDrawerFragment.setUp(R.id.navigation_drawer, drawerLayout, drawerItems);
}

@Override
public void onNavigationDrawerItemSelected(int itemlabelId) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.realcontent, CustomFragmentManager.newInstance(itemlabelId)).commit();
}

NavigationDrawerFragment.java NavigationDrawerFragment.java

View rootView = (RelativeLayout) inflater.inflate(R.layout.layout_fragment_drawer, container, false);
ListView mDrawerListView = (ListView) rootView.findViewById(R.id.drawer_list);
    LinearLayout footerView = (LinearLayout) rootView.findViewById(R.id.footer);

Your left drawer layout do not necessarily need to be a LinearLayout , it could be something like: 您的左抽屉布局不一定需要是LinearLayout ,它可能是这样的:

<RelativeLayout
    android:id="@+id/left_drawer_layout"
    android:layout_width="@dimen/navigation_drawer_max_width"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/drawer_footer"
        android:layout_gravity="start"
        android:orientation="vertical">

        <!-- YOUR PREVIOUS LAYOUT -->
    </LinearLayout>

    <LinearLayout 
        android.id="@+id/drawer_footer"
        android:layout_width="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_height="100dp">
        <!-- YOUR FOOTER WITH layout_alignParentBottom -->
    </LinearLayout>
</RelativeLayout>

That way your footer will always be on the bottom of your left drawer. 这样您的页脚将始终位于左侧抽屉的底部。

you can use this drawer on your project or some layout like what used on this project https://github.com/neokree/MaterialNavigationDrawer 你可以在你的项目上使用这个抽屉或者像这个项目上使用的那样布局https://github.com/neokree/MaterialNavigationDrawer

project have addBottomSection method for footer layout project为页脚布局添加了addBottomSection方法

you can see example of this project in this apk: https://github.com/neokree/MaterialNavigationDrawer/blob/master/example.apk 你可以在这个apk上看到这个项目的例子: https//github.com/neokree/MaterialNavigationDrawer/blob/master/example.apk

Because your drawerListView 's android:layout_height="match_parent" , so the it's take all height left in scree and push the bottom layout out of screen. 因为你的drawerListViewandroid:layout_height="match_parent" ,所以它在scree中保留所有高度并将底部布局推出屏幕。
You can try this: 你可以试试这个:

<LinearLayout
android:id="@+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/activeChatsList" />

    <ListView
        android:id="@+id/drawerListView" />
</LinearLayout>

<Button
    android:id="@+id/CancelButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="“Profile" />

</LinearLayout>

Beside of that, I suggest you should use 1 list view with multi viewType in adapter instead of 2 separate list views because if the 1st scrollable then the 2nd will not show up or if the 2nd show up, that mean the 1st doesn't need to scroll so it's meaningless to be a listview. 除此之外,我建议你应该在适配器中使用1个带有多个viewType的列表视图而不是2个单独的列表视图,因为如果第一个可滚动,那么第二个不会显示或第二个显示,这意味着第一个不需要滚动所以成为列表视图毫无意义。
Hop this helps. 跳这有帮助。

最好的方法是为页脚创建另一个布局,并将该布局添加到列表页脚。

listView.addFooterView(R.layout.<your footer layout>);

This is only suggestion. 这只是建议。 I am adding this as my answer because I dont have enough reputation to add a comment on others posts, Sorry.I think this is already solved in SO. 我添加这个作为我的答案,因为我没有足够的声誉来添加评论其他帖子,抱歉。我认为这已经在SO中解决了。 Please check the below this link How to put list items at the bottom of list view in Navigation Drawer like Foursquare 请查看下面的链接如何将列表项放在导航抽屉列表视图的底部,如Foursquare

you can use ActionBarSherlock library. 你可以使用ActionBarSherlock库。 I think It is so helpfull. 我认为这是非常有帮助的。

You have to use frame layout after the drawer control tag. 您必须在抽屉控制标签后使用框架布局

Place all controls in the frame layout. 将所有控件放在框架布局中。

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

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