Android 在 LinearLayout 上仅添加顶部和底部边框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23216305/
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 06:54:10  来源:igfitidea点击:

Add only top and bottom border on LinearLayout

androidandroid-linearlayoutandroid-shape

提问by wawanopoulos

I would like to add only a bottom and a top border on my Linearlayout. I have tried to do this :

我只想在我的Linearlayout. 我试过这样做:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:bottom="1dp"
    android:top="1dp">
    <shape android:shape="rectangle">
        <solid android:color="#FFFFFF" />
        <stroke
            android:width="1dp"
            android:color="#000" />
    </shape>
</item>
</layer-list>

But it add a border around the shape..

但它在形状周围添加了一个边框..

Could you help me please ?

请问你能帮帮我吗 ?

回答by Tony Vu

I think you can create this drawable and use it as background:

我认为您可以创建此 drawable 并将其用作背景:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
   <item>
        <shape android:shape="rectangle" >
            <solid android:color="#000"/>
        </shape>
    </item>
    <item android:bottom="1dp" android:top="1dp">
        <shape android:shape="rectangle" >
            <solid android:color="#FFFFFF" />
        </shape>
    </item>
</layer-list>

Think of is as drawing a rectangle with border color first and then lay on top of it a rectangle with your background color leaving out 1dp on top and at the bottom.

想象一下,首先绘制一个带有边框颜色的矩形,然后在它上面放置一个带有背景颜色的矩形,在顶部和底部留下 1dp。

回答by Jay Patel

Make this two fileand put this code. you can set border top and bottom border,

制作这两个文件并放置此代码。您可以设置边框顶部和底部边框,

main.xml

主文件

<TextView
      android:text="This is textline"
      android:background="@drawable/border_set"
/>

border_set.xml

border_set.xml

This file located into full path project_root/res/drawable/border_set.xml

此文件位于完整路径中 project_root/res/drawable/border_set.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
      <shape 
        android:shape="rectangle">
            <stroke android:width="1dp" android:color="#FF000000" />
            <solid android:color="#FFDDDDDD" />

        </shape>
   </item>

   <item android:top="1dp" android:bottom="1dp"> 
      <shape 
        android:shape="rectangle">
            <stroke android:width="1dp" android:color="#000" />
            <solid android:color="#FFFFFF" />
        </shape>
   </item>

</layer-list>

回答by dzikovskyy

Here is the solution. It works even with transparent background.

这是解决方案。它即使在透明背景下也能工作。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:left="-2dp"  android:right="-2dp">
        <shape android:shape="rectangle">
            <stroke android:width="2dp" android:color="@color/borderColor" />
            <solid android:color="@color/backgroundColor" />
        </shape>
    </item>

</layer-list>

回答by doctorram

I believe this is the simplest way:

我相信这是最简单的方法:

<View
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:background="#000000" />

回答by Sercho

This is my version; top border and bottom border are visible, not showing the left or right borders. And the background is transparent.

这是我的版本;上边框和下边框可见,不显示左右边框。而且背景是透明的。

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

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="1dp">

    <item
        android:left="-1dp"
        android:right="-1dp"
        android:top="-1dp"
        android:bottom="1dp">
        <shape
            android:shape="rectangle">
            <stroke
                android:width="1dp"
                android:color="@color/BlueGrey_colorPrimary" />
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>

</layer-list>

回答by Keith Davis

A quick way to achieve this:

实现这一目标的快速方法:

  • Add a Text View to the bottom and/or top of your layout.
  • Set the TextView's width to "match_parent"
  • Set the TextView's height to about "1dp" or find the thickness you would like
  • Set the TextView's background to the color you would like the border to be
  • 将文本视图添加到布局的底部和/或顶部。
  • 将 TextView 的宽度设置为“match_parent”
  • 将 TextView 的高度设置为“1dp”左右或找到您想要的厚度
  • 将 TextView 的背景设置为您希望边框的颜色

I hope this helps!

我希望这有帮助!

回答by extmkv

Its simple. Draw 3 shapes like this.

这很简单。像这样画3个形状。

<?xml version="1.0" encoding="utf-8"?>
  <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <shape android:shape="rectangle" >
            <solid android:color="@color/menu_line_separator_in" />
        </shape>
    </item>
    <item android:bottom="1.5dp">
        <shape android:shape="rectangle" >
            <solid android:color="@color/menu_line_separator_out" />
        </shape>
    </item>
    <item android:top="1.5dp">
        <shape android:shape="rectangle" >
            <solid android:color="@color/menu_line_separator_out" />
        </shape>
    </item>

</layer-list>