如何在导航抽屉中居中自定义布局?

麻麻

我创建了一个导航抽屉并列出了菜单图标。之后,我必须在菜单列表的底部包含一个自定义布局。

活动_main.xml

    <?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:layout_gravity="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) -->
        <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"
                ndroid:layout_height="?attr/actionBarSize"
                app:titleTextColor="@color/white"
                android:background="?attr/colorPrimary"
                ndroid: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/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:menu="@menu/drawer_view_menu"
        app:headerLayout="@layout/nav_header"/>
 </android.support.v4.widget.DrawerLayout>

drawer_view_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_prof"
            android:title="@string/my_profile" />
           ........other items here.......
        </group>
      <group android:checkableBehavior="single">
        <item app:actionLayout="@layout/toggle_button"
            android:title="@string/blank"
            />
    </group>

切换按钮.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/left_rounded"
    android:backgroundTint="@color/red"
    android:text="@string/lang_en"
    android:textColor="@color/white"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/right_rounded"
    android:text="@string/lang_ar" />

然后它显示了自定义布局,但它在右侧对齐。我需要将其水平居中对齐。下面还附上了一张问题图。

问题说明

在此处输入图片说明

麻麻

在互联网上进行了一些研究后,我想出了一个解决方案。

脚步

  1. 在 NavigationView 布局中包含我的自定义布局(toggle_button.xml)。

导航视图布局

 <?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:layout_gravity="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) -->
        <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"
                ndroid:layout_height="?attr/actionBarSize"
                app:titleTextColor="@color/white"
                android:background="?attr/colorPrimary"
                ndroid: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/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:menu="@menu/drawer_view_menu"
        app:headerLayout="@layout/nav_header">

     <include layout="toggle_button.xml""/>

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

2.更新了我的自定义布局,使底部的切换居中对齐。

切换按钮.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:layout_marginBottom="@dimen/margin_medium"
    android:layout_gravity="bottom">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/left_rounded"
        android:backgroundTint="@color/red"
        android:text="@string/lang_en"
        android:textColor="@color/white"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/right_rounded"
        android:text="@string/lang_ar" />

</LinearLayout>
  1. 还从drawer_view_menu.xml 中删除了第二组

完毕!就这么简单......

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Android中创建自定义导航抽屉

如何在android中制作自定义导航抽屉

如何在导航抽屉中设置自定义图标?

如何在抽屉导航器中添加自定义抽屉,反应导航4.x?

如何创建自定义导航抽屉?

反应导航中的自定义抽屉

React Native 抽屉导航:如何通过单击特定场景中的自定义按钮打开抽屉?

如何在主布局中显示自定义布局?

如何在 UITableViewCell 中居中自定义 CAShapeLayer?

如何自定义导航抽屉项目高度

无法在自定义导航抽屉中设置按钮的text

自定义导航抽屉中setOnClickListener的最佳做法

如何创建自定义导航抽屉,该抽屉将在抽屉onitemclick旁边打开ListView?

如何使用基于抽屉导航的自定义组件从侧边菜单中获取价值

如何在Android中创建自定义通知布局?

如何在Windows中删除自定义键盘布局?

如何在java中制作自定义嵌套布局

如何在mvvmlight中实现自定义导航服务

如何在NativeScript中创建自定义导航栏?

如何在导航栏中包含自定义开关

如何在swiftui中自定义导航栏

反应本机自定义抽屉导航

带自定义按钮的导航抽屉

如何在导航抽屉布局中添加图像标题

如何创建自定义半底导航布局

如何使用React Navigation v.5中的DrawerItemList在抽屉式导航器中传递自定义按钮?

如何在React Native中从自定义导航器导航到createBottomTabNavigator?

自定义 Listview 不显示在导航抽屉的片段中,具有自定义 arrayadaptar 类

如何在RecyclerView Android Studio中以自定义线性布局自定义单个元素的方向?