【问题标题】:User Accounts in Navigation Drawer导航抽屉中的用户帐户
【发布时间】:2015-08-16 12:01:53
【问题描述】:

我已经使用新的设计支持库成功设置了导航抽屉,但不知道如何进一步进行。 我需要提供用户帐户,有人可以提供代码或如何完成的想法。

这是我目前拥有的代码。

<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<RelativeLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

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

</RelativeLayout>


<android.support.design.widget.NavigationView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/header"
    app:menu="@menu/nav_menu" />

【问题讨论】:

    标签: android navigation account drawer


    【解决方案1】:

    假设您已设置 SQLite 帮助程序类 (example),并且知道使用 HashMap 获取用户详细信息,您可以通过在导航抽屉 xml 上添加几个视图轻松完成此操作。

    您需要添加一个 textview 和一个 circleimageview from hdondenhof,以显示您的用户名和个人资料图片。如果您想显示更多视图,您也可以这样做。为它们分配 ID,以便您可以在对应的 java 中显示所需的信息。

    在您的导航抽屉片段中,如果用户名和图片参数已经存储在您的 SQLite 数据库中,您可以在此处传递用户名和图片参数。就我而言,我通常会在我的片段的 onCreateView 中调用它:

    // Initialize and declare SQLite handler class to retrieve user's details:
    final SQLiteHandler sqLiteHandler = new SQLiteHandler(getActivity());
    final HashMap<String, String> user = sqLiteHandler.getUserDetails();
    
    // Retrieving the user's username for display on navUsername
    String setUsername = user.get("username");
    navUsername = (TextView) view.findViewById(R.id.nav_username);
    navUsername.setText("Welcome, " + setUsername);
    

    真的就是这么简单。 circleimageview 的工作方式与 imageview 类似,因此您可以从数据库中获取用户的图片,或者如果没有找到图片则显示一个可绘制对象。

    【讨论】:

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