安卓导航抽屉垂直边框

5

我想让我的 NavigationDrawer 看起来像这个应用程序中的一样:

enter image description here

更具体地说,我想在右侧添加那个小的 1 像素边框:

enter image description here

但是这是我得到的抽屉效果图:

enter image description here

这是抽屉片段的 XML 代码:

<ListView 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:background="#E7E7E7"
android:choiceMode="singleChoice"
android:dividerHeight="0.5dp" />

我需要做什么?
7个回答

4

试试这个:

DrawerLayout mDrawerLayout;

onCreate(Bundle ...){
    //...
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
                    GravityCompat.START);
    //...
}

编辑

您需要在ListView旁边添加一个View,并将两者与一个水平的LinearLayout包装,并给两者一些权重。我在这里为listView分配了0.99的权重,为view分配了0.01的权重,您可以根据需要进行更改。

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <ListView android:id="@+id/drawer_list"
            android:layout_weight="0.99"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#E7E7E7"
            android:choiceMode="singleChoice"
            android:dividerHeight="0.5dp" />

        <View
            android:layout_weight="0.01"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="#eeccbb"/>
    </LinearLayout>

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

@SartherisStormhammer 或许你可以尝试在 XML 中添加 view - Apurva
抽屉的布局只是一个单独的ListView,就像我在我的问题中展示的那样。 - Sartheris Stormhammer
这些都很酷,但现在我遇到了一些大错误。“03-04 17:03:59.269:E / AndroidRuntime(2113):由于java.lang.ClassCastException:android.support.v4.widget.DrawerLayout无法转换为android.widget.ListView而引起。” - Sartheris Stormhammer
@SartherisStormhammer,我建议你查看Atul在这个问题上的答案,它与你遇到的问题相同。 - Apurva

2

enter image description here

您可以在10秒内完成此操作。 在xml中使用图层列表,并将其设置为listview的背景 并给Listview右侧1dp的填充

用您的垂直边框颜色替换#ff0000(红色) 和#00c0c0与您的listview颜色。 不要忘记在listview中给出右侧1dp的填充。

Layerlist xml

<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item >

    <shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="#ff0000"/>


</shape>

</item>

<item 
    android:right="1dp" >

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

   <solid android:color="#00c0c0"/>




</shape>

</item>

</layer-list>

你基本上将透支值翻了一倍。 - Vikram
但他已经指定了片段的XML为ListView。根据其他答案,添加布局或视图将会增加开销。 - Umesh Chhabra
不适用于使用了九宫格图的情况。 - Vikram
你必须为不同的屏幕尺寸准备不同大小的9patch图像。 - Umesh Chhabra
“你必须为不同的屏幕尺寸准备不同大小的9patch图像。”这是错误的。9patch的整个意义在于它们可以适应屏幕/视图大小。你需要做的是准备多个9patch可绘制对象来处理不同的屏幕密度。 - Vikram
是的,你只需要以正确的方式编写相同的内容。 - Umesh Chhabra

1

我希望找一个知道自己在做什么的人,而不是从互联网上随意复制答案的人。

嗯,你不会比九路径更好:

enter image description here

上面的9-patch图案有一个0.5dp(基准xhdpi)黄色边框,仅供说明。顶部和左侧的保护线(黑线)定义了可拉伸区域。
请注意,右侧的保护线完全延伸到末端。这意味着顶部和底部不会有任何填充。但是,底部的保护线从最左边的像素开始,直到黄色边框之前结束。当您将此9patch设置为列表视图时,底部的保护线将确保向右填充0.5dp(黄色边框的宽度)。
但是,这种方法确实需要一些工作。您需要创建与存储桶数量相同的9patch可绘制对象。如果您需要0.5dp的边框,则每个存储桶的黄色边框宽度将不同:
xxxhdpi:   2 px
xxhdpi:  1.5 px
xhdpi:     1 px
hdpi:   0.75 px
.... and so on

关于9patch可绘制对象的更多信息:链接

另一种方法(已在此处建议)是使用一个View作为边框。在这种情况下,最轻的排列方式(没有在此处提到)是使用FrameLayout并将ListView和边框放置其中:

<FrameLayout 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">
    <!-- marginRight set to 0.5dp -->
    <ListView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginRight="0.5dp"
        android:background="#E7E7E7"
        android:choiceMode="singleChoice"
        android:dividerHeight="0.5dp" />

    <!-- gravity set to right & background set to border color -->
    <View
        android:layout_width="0.5dp"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:background="@color/some_color"/>

</FrameLayout>

在这种情况下,您最终会有一个ViewGroup和一个View作为装饰。 9patch将是最佳解决方案。随便挑选。

0

预计完成时间:Apurva的解决方案看起来更简洁。

我对导航抽屉不是很熟悉,但是您可以在ListView之后向DrawerLayout添加一个视图来创建垂直分隔线。

<View
        android:layout_height="fill_parent"
        android:layout_width="2dip"
        android:background="@android:color/some_color" />

0

你需要将背景设置为一个带有右边框的自定义可绘制对象,就像这样:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:top="-5dp" android:bottom="-5dp" android:left="-5dp">
        <shape>
            <solid android:color="@android:color/transparent" />
            <stroke android:width="4dp" android:color="#000000" /> <!--BORDER COLOR-->
        </shape>
    </item>

    <item android:right="5dp">
        <shape android:shape="rectangle">
            <solid android:color="#E7E7E7" />
        </shape>
    </item>
</layer-list>

我在边框上使用了随机dp,将其设置为5dp并将边框的颜色设为黑色,根据您的需要更改这些!


0

试一下这个

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <ListView android:id="@+id/drawer_list"
            android:layout_weight="0.99"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#E7E7E7"
            android:choiceMode="singleChoice"
            android:dividerHeight="0.5dp" />

        <View
            android:layout_weight="0.01"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="#eeccbb"/>
    </LinearLayout>

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

在YouTube上观看视频 尝试这个设计材料githup链接


0
setDrawerShadow() 方法用于将任何自定义的 drawable/resource 设置为导航抽屉的左/右边框。
您可以查看:导航抽屉 -> setDrawerShadow() API 使用信息 您需要做两件事:
(1)在 res -> colors.xml 中定义您想要的颜色作为 drawable 元素。
<drawable name="drawer_shadow">FFE7E7E7</drawable>

(2)通过将其指定为抽屉阴影可绘制项,在MainActivity.java中使用此元素:

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

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

MainActivity.java

onCreate(Bundle mSavedBundle) {
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
                GravityCompat.START);
} 

这将把颜色“FFE7E7E7”作为导航抽屉左/右边缘的可绘制对象添加。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接