Add only top and bottom border on LinearLayout

╄→尐↘猪︶ㄣ 提交于 2019-12-31 18:56:12

问题


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

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


回答1:


Make this two file and 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

This file located into full path 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>



回答2:


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

<?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.




回答3:


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>



回答4:


I believe this is the simplest way:

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



回答5:


My version is this, only is visible the border top and bottom, not shows the borders left nor right. 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>



回答6:


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

I hope this helps!




回答7:


You can follow this link Is there an easy way to add a border to the top and bottom of an Android View?

I expect that you are solve from this link. also you can solve How to add border around linear layout except at the bottom?




回答8:


Its simple. Draw 3 shapes like this.

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


来源:https://stackoverflow.com/questions/23216305/add-only-top-and-bottom-border-on-linearlayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!