导航抽屉与android中的用户标题

时间:2014-12-18 13:26:38

标签: android navigation-drawer

尝试获取此信息(图片标题):enter image description here

这是我的代码:

<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:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">

<include layout="@layout/toolbar" />

<android.support.v4.widget.DrawerLayout
    android:layout_width="match_parent"
    android:id="@+id/drawerLayout"
    android:layout_height="match_parent">

    <!-- activity view -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:background="#fff"
        android:layout_height="match_parent">

        <TextView
            android:layout_centerInParent="true"
            android:layout_width="wrap_content"
            android:textColor="#000"
            android:text="Activity Content"
            android:layout_height="wrap_content" />

        <com.shamanland.fab.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"
            app:floatingActionButtonColor="#000000"
            app:floatingActionButtonSize="mini"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_marginRight="16dp"
            android:layout_marginBottom="20dp"
            />
    </RelativeLayout>

    <!-- navigation drawer -->
    <RelativeLayout
        android:layout_gravity="left|start"
        android:layout_width="match_parent"
        android:background="#fff"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/top_control_bar">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="bababababbababababbababababba"/>
        </RelativeLayout>

        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:divider="#eee"
            android:background="#fff"
            android:dividerHeight="1dp" />
    </RelativeLayout>

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

我不知道是否必须在列表或其他内容上创建新的布局。

我已经看到android中有一个listview标题,但这在我的例子中并不起作用。 谢谢你的帮助

2 个答案:

答案 0 :(得分:0)

您需要创建一个包含List的简单LinearLayout。这是一个例子:

 <LinearLayout
android:id="@+id/linearDrawer"
android:layout_width="260dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/navigation_items"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout
    android:id="@+id/userContent"
    android:layout_width="260dp"
    android:layout_height="150dp"
    android:background="@color/black"
    android:gravity="center_vertical"
    android:paddingRight="10dp">

    <RelativeLayout
        android:id="@+id/userDrawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/transparent">

        <ImageView
            android:id="@+id/ImgDrawer"
            android:layout_width="260dp"
            android:layout_height="200dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:contentDescription="@string/dummy_user_description"
            android:scaleType="centerCrop"
            android:src="@drawable/ic_user" />

        <RelativeLayout
            style="@style/textView_title_list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true">

            <TextView
                android:id="@+id/txt_user_name_drawer"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/dummy_username"
                android:textAllCaps="true"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/white" />

            <TextView
                android:id="@+id/txt_user_lastname_drawer"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/txt_user_name_drawer"
                android:text="@string/dummy_user_description"
                android:textAllCaps="false"
                android:textColor="@color/white"
                android:textSize="12sp" />
        </RelativeLayout>
    </RelativeLayout>
</LinearLayout>

<View
    android:id="@+id/viewSeparator"
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:background="@color/blue_dark" />

<ListView
    android:id="@+id/listDrawer"
    android:layout_width="260dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/navigation_items"
    android:cacheColorHint="@color/transparent"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="1dp" />

布局本身包含FrameLayout(内容)和抽屉。

样品:

 <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"
tools:context="com.androiddevworker.rateit.view.RatingActivity">

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

<fragment
    android:id="@+id/navigation_drawer"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_height="fill_parent"
    android:layout_gravity="start"
    android:name="com.androiddevworker.rateit.view.fragments.RatingNavigationDrawerFragment"
    tools:layout="@layout/navigation_main_include" />

在您的活动中,您可以使用

获取它
    private RatingNavigationDrawerFragment mRatingNavigationDrawerFragment;

    mRatingNavigationDrawerFragment = (RatingNavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);

而RatingNavigationDrawerfragment(示例)只是一个简单的片段。

答案 1 :(得分:0)

尝试以下代码......

  <RelativeLayout
    android:layout_gravity="left|start"
    android:layout_width="match_parent"
    android:background="#fff"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/top_control_bar">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="bababababbababababbababababba"/>
    </RelativeLayout>

     <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/top_control_bar1">
        <ImageView
                    android:id="@+id/imageView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/ur_image" />
      <TextView
                    android:id="@+id/textView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="start"
                    android:text="Your Text"
                    android:textColor="#FFFFFF"
                    android:textSize="38px" />

    </RelativeLayout>

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/top_control_bar1"
        android:divider="#eee"
        android:background="#fff"
        android:dividerHeight="1dp" />
</RelativeLayout>