【问题标题】:Adding a button to footer of navigation drawer list android在导航抽屉列表android的页脚添加一个按钮
【发布时间】:2017-08-29 19:55:31
【问题描述】:

我有一个使用抽屉布局和适配器制作的导航抽屉。我想在侧面菜单的底部添加一个注销按钮。我经历了SO,但无法解决。主 xml 文件在抽屉布局中有一个列表视图。

         <?xml version="1.0" encoding="utf-8"?>
    <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">

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
    android:weightSum="5"
     <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1.5"></LinearLayout>

   <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2"
    android:layout_gravity="center"
    android:orientation="vertical">

        <Button
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/buttonbackground"
        android:text="PHOTOS"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
        android:textColor="@color/white" />

        <Button
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:background="@drawable/buttonbackground"
        android:text="VIDEOS"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
        android:textColor="@color/white" />

        <Button
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:background="@drawable/buttonbackground"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
        android:text="HISTORY"
        android:textColor="@color/white" />

        </LinearLayout>
        <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="0dp"
        android:layout_weight="1.5"></LinearLayout>

         </LinearLayout>

        <RelativeLayout
         android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
        android:id="@+id/navList"
        android:layout_width="320dp"
        android:layout_height="match_parent"
        android:layout_above="@+id/logout"
        android:layout_gravity="left|start"
        android:background="#ffeeeeee"></ListView>

        <Button
        android:id="@+id/logout"
        android:layout_width="320dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="LogOut" />
         </RelativeLayout>
         </android.support.v4.widget.DrawerLayout>
        <android.support.v4.widget.DrawerLayout/>

并且列表在 java 代码中被夸大了。

     private void addDrawerItems() {
    String items[]={"Home","Video","Camera","History","Version"};
    mAdapter=new ArrayAdapter<>(this,R.layout.listview_drawer_item_row,items);
    mDrawerList.setAdapter(mAdapter);
}

如何在菜单底部添加按钮?

【问题讨论】:

  • 创建自定义布局并将布局扩展至您的抽屉
  • 我相信每一行的布局都不会?不确定
  • @Harshita 检查此链接stackoverflow.com/questions/21796209/…
  • 将按钮添加为来自 java 文件的列表视图的页脚视图。
  • 正如 Guptsa 所说,创建一个 ArrayAdapter 来扩展您的布局并验证当前项目是否是最后一个项目并扩展按钮。

标签: android navigation-drawer


【解决方案1】:

你能试试这个吗,它是一种解决方法,但我希望它能提供一些解决你问题的想法

 <android.support.v4.widget.DrawerLayout android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left|start">
        <ListView
            android:id="@+id/navList"
            android:layout_width="320dp"
            android:layout_height="match_parent"
            android:layout_above="@+id/logout"
            android:layout_gravity="left|start"
            android:background="#ffeeeeee"></ListView>

        <Button
            android:id="@+id/logout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:text="LogOut" />
    </RelativeLayout>

【讨论】:

  • 这显示了按钮,但现在,它不允许我滑动菜单。菜单按原样覆盖屏幕。
  • 你能发一个屏幕截图吗?
  • 所以,你不能向左滑动?
  • 不,我不能刷卡
  • 你能发布你的完整布局xml吗?
【解决方案2】:

根据我的经验和知识,您需要将注销按钮添加为列表视图的页脚视图,或者将其设置为导航视图的底部。你不能把你的按钮放在你的导航视图之外。如果您设法实现类似的目标,您将只能滚动导航视图,而不能滚动注销按钮。我认为它不会随着您的 navigationView 滚动。

为此,请根据您的要求制作自定义布局并将其设置为页脚视图,或者将其设置在导航视图的底部。

【讨论】:

    猜你喜欢
    • 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
    相关资源
    最近更新 更多