繁体   English   中英

导航抽屉中的用户帐户

[英]User Accounts in Navigation Drawer

我已经使用新的设计支持库成功设置了导航抽屉,但不确定如何继续操作。 我需要提供用户帐户,有人可以提供代码或有关如何执行此操作的想法。

导航抽屉

这是我目前拥有的代码。

<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" />

假设您已设置SQLite帮助器类( 示例 ),并且具有使用HashMap获取用户详细信息的知识,则可以通过在导航抽屉xml上添加几个视图来轻松完成此操作。

您需要从hdondenhof添加文本视图和circleimageview ,以显示您的用户名和个人资料图片。 而且,如果您希望显示更多视图,也可以这样做。 为它们分配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一样,因此您可以从数据库中获取用户的图片,如果找不到图片,则显示可绘制的图片。

暂无
暂无

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

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