其他分享
首页 > 其他分享> > 为什么我的Android导航抽屉开得太宽?

为什么我的Android导航抽屉开得太宽?

作者:互联网

我有一个相当基本的导航抽屉工作得很好 – 一个简单的ListView.但是我需要一个可选项目上方的标题,所以(见下文)修改了抽屉的XML,使其成为包含标题TextView的RelativeLayout,然后是项目的ListView.

结果很奇怪.即使我已经指定所有3(RelativeLayout,TextView和ListView)的宽度为240dp,这是ListView表示整个抽屉的XML时的宽度,它看起来像这样(忽略音量控制 – 没看到弹出).请注意,ListView的宽度为240 dp,但我指定的红色背景一直向右移动.

enter image description here

这是我的第一个屏幕的XML.相关的抽屉XML位于底部.

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/ail_background_gradient"
    tools:context="com.allinlearning.assist_android.HomeScreenActivityFragment">

    <ImageView
        android:id="@+id/imgViewLogo"
        android:src="@drawable/ail_logo"
        android:layout_margin="10dp"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:scaleType="fitXY"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="ALL In Learning"
        android:id="@+id/textViewLogo"
        android:layout_margin="10dp"
        android:layout_below="@+id/imgViewLogo"
        android:layout_centerHorizontal="true"
        android:textSize="@dimen/font_size26"
        android:textStyle="bold" />

    <ImageButton
        android:id="@+id/imgBtnGradeAssessment"
        android:src="@drawable/grade_assessment"
        android:layout_width="100dp"
        android:layout_height="95dp"
        android:scaleType="fitXY"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/textViewGradeAssessment"
        android:layout_toStartOf="@+id/textViewGradeAssessment" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Grade"
        android:id="@+id/textViewGradeAssessment"
        android:textColor="@color/white"
        android:textSize="@dimen/font_size28"
        android:layout_centerVertical="true"
        android:layout_alignRight="@+id/imgViewLogo"
        android:layout_alignEnd="@+id/imgViewLogo" />

    <ImageButton
        android:id="@+id/imgBtnPrivateData"
        android:src="@drawable/two_clickers"
        android:layout_width="100dp"
        android:layout_height="95dp"
        android:scaleType="fitXY"
        android:layout_below="@+id/imgBtnGradeAssessment"
        android:layout_alignLeft="@+id/imgBtnGradeAssessment"
        android:layout_alignStart="@+id/imgBtnGradeAssessment" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Private Data"
        android:id="@+id/textViewPrivateData"
        android:textColor="@color/white"
        android:textSize="@dimen/font_size28"
        android:layout_alignBottom="@+id/imgBtnPrivateData"
        android:layout_toRightOf="@+id/imgBtnPrivateData"
        android:layout_toEndOf="@+id/imgBtnPrivateData"
        android:layout_marginBottom="40dp" />

</RelativeLayout>

<!-- The navigation drawer -->
<LinearLayout android:id="@+id/left_drawer"
    android:orientation="vertical"
    android:background="@color/red"
    android:layout_width="240dp"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="240dp"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="I AM THE TITLE"
        android:id="@+id/tvDrawerTitle"
        android:layout_margin="10dp"
        android:layout_centerHorizontal="true"
        android:textSize="@dimen/font_size26"
        android:textStyle="bold"
        android:textAlignment="center"
        android:textColor="@color/black" />

    <ListView
        android:id="@+id/lvDrawerItems"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="@color/white" />

</LinearLayout>

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

解决方法:

layout_gravity属性确定哪个子视图充当DrawerLayout中的抽屉.目前,您的DrawerLayout的直接子项都没有该属性集,因此两者都只是填充它,LinearLayout位于顶部,覆盖内容RelativeLayout.

将android:layout_gravity =“left”从ListView移动到LinearLayout.

标签:android,android-layout,navigation-drawer,background-color
来源: https://codeday.me/bug/20190928/1825629.html