如何在线性布局的顶部绘制边框

13

我按照渐变分隔符中的示例进行了操作:http://www.connorgarvey.com/blog/?p=34

我试图在我的线性布局底部绘制一条水平线。

这是我的线性布局文件:

        <LinearLayout android:id="@+id/test" android:layout_width="fill_parent"
            android:layout_height="wrap_content>

<ImageView android:id="@+id/icon1"
    android:layout_width="32dip"
    android:layout_height="32dip"
/>

我添加了

<View
android:background="@drawable/black_white_gradient"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_above="@id/test"
/>

但是我在LinearLayout的上面并没有看到任何线条。当我去Hierarchy View查看视图(对于水平分隔符),getWidth()为0,而getHeight()为1。

请问我错过了什么吗?

谢谢。

4个回答

4

你所发布的代码没有问题,但是大小为1dp可能不太明显。你可以增加大小或者使用默认的单行可绘制对象。

android:background="@android:drawable/divider_horizontal_bright" 
// this for white background only

我希望这能帮助你获得你想要的东西。


2
我认为父级linearlayout视图中可能缺少方向设置;
<LinearLayout android:orientation="vertical"

如果你使用RelativeLayout而不是LinearLayout,你可以设置's layout_alignParentBottom;

<View android:layout_alignParentBottom="true"

1

只有当大小至少为2dp时才会显示黑色边框。尝试移除边框,并将纯色设置为黑色:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#000" />
</shape>

那么你可以再次使用1dp的高度。


1

如果高度为1dp,则不会可见。请尝试使用2dp:

<View android:background="@drawable/black_white_gradient" android:layout_width="fill_parent" android:layout_height="2dp" android:layout_above="@id/test" />


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