【问题标题】:How to change the drawer title?如何更改抽屉标题?
【发布时间】:2018-11-05 08:46:56
【问题描述】:

我是 Android 新手,正在开发一个应用程序。我正在尝试开发一个具有抽屉布局的应用程序。一切都很好,但我无法更改屏幕截图上标记的抽屉菜单的标题。

这是为您提供方便的布局文件。我从这个link得到了帮助:

<?xml version="1.0" encoding="utf-8"?>
<!-- Use DrawerLayout as root container for activity -->
<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">

    <!-- Layout to contain contents of main body of screen (drawer will slide over this) -->
    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/user_home_main_view">

        <Button
            android:id="@+id/button_mutual_funds_icl"
            style="@style/Widget.AppCompat.Button"
            android:layout_width="252dp"
            android:layout_height="49dp"
            android:layout_marginTop="120dp"
            android:layout_marginEnd="16dp"
            android:text="Mutual Funds of ICL"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/icon_mutual_funds_of_icl"
            android:layout_width="51dp"
            android:layout_height="49dp"
            android:layout_marginStart="28dp"
            android:layout_marginTop="120dp"
            android:layout_marginEnd="8dp"
            app:layout_constraintEnd_toStartOf="@+id/button_mutual_funds_icl"
            app:layout_constraintHorizontal_bias="0.384"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/mutual_funds_of_icl" />

        <Button
            android:id="@+id/button_current_nav"
            android:layout_width="252dp"
            android:layout_height="49dp"
            android:layout_marginTop="32dp"
            android:layout_marginEnd="16dp"
            android:text="Current NAVs"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/button_mutual_funds_icl" />

        <ImageView
            android:id="@+id/icon_current_nav"
            android:layout_width="51dp"
            android:layout_height="49dp"
            android:layout_marginStart="28dp"
            android:layout_marginTop="32dp"
            android:layout_marginEnd="8dp"
            app:layout_constraintEnd_toStartOf="@+id/button_current_nav"
            app:layout_constraintHorizontal_bias="0.458"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/icon_mutual_funds_of_icl"
            app:srcCompat="@drawable/net_asset_value" />

        <Button
            android:id="@+id/button_fund_performance"
            android:layout_width="250dp"
            android:layout_height="49dp"
            android:layout_marginTop="32dp"
            android:layout_marginEnd="16dp"
            android:text="Fund Performance"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/button_current_nav" />

        <ImageView
            android:id="@+id/icon_fund_performance"
            android:layout_width="51dp"
            android:layout_height="49dp"
            android:layout_marginStart="28dp"
            android:layout_marginTop="26dp"
            android:layout_marginEnd="8dp"
            app:layout_constraintEnd_toStartOf="@+id/button_fund_performance"
            app:layout_constraintHorizontal_bias="0.448"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/icon_current_nav"
            app:srcCompat="@drawable/fund_performance" />

        <Button
            android:id="@+id/button_how_to_invest"
            android:layout_width="249dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="28dp"
            android:layout_marginEnd="16dp"
            android:text="How to Invest"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/button_fund_performance" />

        <ImageView
            android:id="@+id/icon_how_to_invest"
            android:layout_width="53dp"
            android:layout_height="49dp"
            android:layout_marginStart="28dp"
            android:layout_marginTop="36dp"
            android:layout_marginEnd="8dp"
            app:layout_constraintEnd_toStartOf="@+id/button_how_to_invest"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/icon_fund_performance"
            app:srcCompat="@drawable/how_to_invest" />

        <Button
            android:id="@+id/user_home_button_invest_now"
            android:layout_width="214dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="141dp"
            android:layout_marginEnd="8dp"
            android:background="@color/colorPrimary"
            android:text="Invest Now"
            android:textColor="@color/colorAccent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.513"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/button_how_to_invest" />

    </android.support.constraint.ConstraintLayout>

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

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar" />

    </FrameLayout>

    <!-- Container for contents of drawer - use NavigationView to make configuration easier -->
    <android.support.design.widget.NavigationView
        android:id="@+id/user_nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:menu="@menu/drawer_view"
        app:headerLayout="@layout/nav_header"/>


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

【问题讨论】:

  • 你能发布你是如何生成抽屉的吗?
  • 如果你是通过 Android Studio 内置模板生成的,那么会为 Drawer 的头部显式创建一个布局文件。在该布局中,您可以为文本字段分配一个 id 并在您的活动中检索它。在那里您可以将文本设置为您想要的任何内容。
  • 您甚至可以使用抽屉github.com/rohitksingh/Notely/blob/master/Notely/app/src/main/…的单独布局来解决这个问题

标签: java android android-layout


【解决方案1】:

【讨论】:

  • 感谢您的帮助:)
【解决方案2】:

更多自定义

如果您想对您的抽屉进行更多自定义。您可以为抽屉创建一个布局并将其包含在android.support.design.widget.NavigationView
像这样

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

     ...........
                 <!- Drawer >

     <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="150dp"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:layout_gravity="left"
        >
           <!--- Your custom layout >
          <include layout="@layout/custom_drawer_layout"
              android:id="@+id/filterDrawer"
              android:layout_height="match_parent"
              android:layout_width="match_parent"
              android:layout_gravity="end"
           />

    </android.support.design.widget.NavigationView>


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

然后你可以为custom_drawer_layout创建一个布局

custom_drawer_layout.xml

<RelativeLayout>
     <spinner/>
     <button/>
     <whatever/>
</RelativeLayout>

希望你能明白。
可以看相关项目here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    相关资源
    最近更新 更多