我创建了一个导航抽屉并列出了菜单图标。之后,我必须在菜单列表的底部包含一个自定义布局。
活动_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" />
然后它显示了自定义布局,但它在右侧对齐。我需要将其水平居中对齐。下面还附上了一张问题图。
问题说明
在互联网上进行了一些研究后,我想出了一个解决方案。
脚步
导航视图布局
<?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>
完毕!就这么简单......
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句