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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1923480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:48:46  来源:igfitidea点击:

How to draw a border at top of the linear layout

android

提问by n179911

I have followed the example in for the gradient dividers: http://www.connorgarvey.com/blog/?p=34

我遵循了梯度分隔符的示例:http: //www.connorgarvey.com/blog/?p= 34

I have tried to draw a horizontal line at the BOTTOM of my linear layout.

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

Here is my linear layout file:

这是我的线性布局文件:

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

And I did add

我确实添加了

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

But i don't see any line at the top of the LinearLayout. And when I go to Hierarchy View and see he View (for the hort separator), the getWidth() is 0 while getHeight() is 1.

但是我在 LinearLayout 的顶部没有看到任何行。当我转到层次结构视图并看到他的视图(用于 hort 分隔符)时,getWidth() 为 0,而 getHeight() 为 1。

Can you please tell me what am I missing?

你能告诉我我错过了什么吗?

Thank you.

谢谢你。

采纳答案by hwii77

I think the orientation might be missing in the parent linearlayout view;

我认为父线性布局视图中可能缺少方向;

<LinearLayout android:orientation="vertical"

Or if you use RelativeLayout instead of LinearLayout, you can set 's layout_alignParentBottom;

或者,如果您使用 RelativeLayout 而不是 LinearLayout,则可以设置 's layout_alignParentBottom;

<View android:layout_alignParentBottom="true"

回答by 1HaKr

With the code you posted ,its perfectly fine.But the fact that the size is 1dpmay not be showing it . increase the size or as you want only a line use this default line drawable from android

使用您发布的代码,它完全1dp没问题。但尺寸的事实可能没有显示出来。增加大小或者你只想要一条线,使用这个默认的可从 android 绘制的线

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

I hope this will help you get what you want

我希望这会帮助你得到你想要的

回答by bdhac

It will not be visible if the height is 1dp. Try 2dp instead:

如果高度为 1dp,它将不可见。改为尝试 2dp:

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

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

回答by Ronald

The black border is only shown if the size is at least 2dp. Try removing the border, and set the solid color to black:

仅当尺寸至少为 2dp 时才会显示黑色边框。尝试移除边框,并将纯色设置为黑色:

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

Than you can use a height of 1dp again.

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