Android:不希望ListView使用完整的Drawer

时间:2015-02-11 09:22:08

标签: android listview navigation-drawer

我编写了我的第一个小应用程序。在这个应用程序中,我使用了两个抽屉,一个在右侧,一个在左侧:

<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">

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:id="@+id/relLayout"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:background="#D1933C"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <com.schnapsidee.app.CustomWebView
        android:id="@+id/Text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/space1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/space3"
        android:scrollbars="vertical"
        android:text="@string/fail"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/black" />

    <android.support.v7.widget.Space
        android:id="@+id/space1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="62dp" />


    <android.support.v7.widget.Space
        android:id="@+id/space2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <android.support.v7.widget.Space
        android:id="@+id/space3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="55dp" />

    <TextView
        android:id="@+id/seitenZahl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:textColor="@color/black"
        android:textSize="22sp" />

</RelativeLayout>

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#111"
    android:width="0dp"/>


<ListView
    android:id="@+id/right_drawer"
    android:layout_width="240dp"
    android:layout_height="800dp"
    android:layout_gravity="end"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="@drawable/pergament"
    android:scrollbars="vertical"
    android:drawSelectorOnTop="false"
   />

我用它们像:

mDrawerItemLeft = getResources().getStringArray(R.array.sidebar_array_left);
    mDrawerItemRight = getResources().getStringArray(R.array.sidebar_array_right);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerListLeft = (ListView) findViewById(R.id.left_drawer);
    mDrawerListRight = (ListView) findViewById(R.id.right_drawer);
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
            GravityCompat.START);
    mDrawerListLeft.setAdapter(new Adapter(this, R.layout.drawer_list_item_left,
            mDrawerItemLeft));
    mDrawerListRight.setAdapter(new Adapter(this, R.layout.drawer_list_item_right,
            mDrawerItemRight));
    mDrawerListLeft.setOnItemClickListener(new LeftDrawerItemClickListener());

    mDrawerListRight.setOnItemClickListener(new RightDrawerItemClickListener());

我的问题是,我不希望正确的抽屉使用完整的屏幕高度。我只是希望它使用它的一部分,如图中的红色部分: http://i.stack.imgur.com/IdXAf.jpg

知道我怎么能这样做吗?

提前致谢!

4 个答案:

答案 0 :(得分:2)

导航抽屉不需要专门为ListView,您可以使用任何View / ViewGroup为此目的只需设置gravity =“end”使其成为右侧导航抽屉。

  <ListView
        android:id="@+id/right_drawer"
        android:layout_width="240dp"
        android:layout_height="800dp"
        android:layout_gravity="end"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="@drawable/pergament"
        android:scrollbars="vertical"
        android:drawSelectorOnTop="false" />

将上述代码更改为:

<RelativeLayout
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="end">

    <ListView
        android:id="@+id/right_drawer"
        android:layout_width="match_parent"
        android:layout_height="500dp"
        android:choiceMode="singleChoice"
        android:layout_centerVertical="true" />
</RelativeLayout>

根据需要设置ListView的其他属性,并将高度更改为短于800dp的值,因为800dp的高度非常长,并且肯定会比许多设备上的实际屏幕高度更大。

答案 1 :(得分:0)

为两个视图添加宽度 0dp ,并在两个视图中添加 android:layout_weight =“1”

答案 2 :(得分:0)

使用布局,而不是使用ListView。线性,相对然后将ListView放在该布局中。如果将布局作为xml中的最后一个元素放置,则整个布局将成为导航框的布局。然后,您可以按照自己的方式将Listview放置在布局中。

<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 -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:background="#111"/>
    <!-- The navigation drawer -->
    <RelativeLayout
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start">

        <!-- Position this list view inside the relative layout the way you  want -->
        <ListView android:id="@+id/left_drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"/>
    </RelativeLayout>
</android.support.v4.widget.DrawerLayout>

答案 3 :(得分:0)

在导航栏中使用

android:layout_height="0dp"
android:layout_weight="1"

使用这个,你应该知道的事情......导航抽屉取其内容的高度。所以它采取listView,高度..给你的listview高度(作为你的要求)然后给listView背景“透明,如下所示

 android:layout_height="as you want"
 android:background="@color/tranparent"

将其设置在标题下,然后采用线性布局并在标题后写入导航抽屉,s代码..如果您是相对布局,则在导航抽屉中使用android:layout_below="your header id"