Android 除了底部之外,如何在线性布局周围添加边框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10457135/
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
How to add border around linear layout except at the bottom?
提问by Damir
How to add border around linear layout except at the bottom ? LinearLayout needs to have border at left, top and right side but not at the bottom.
除了底部之外,如何在线性布局周围添加边框?LinearLayout 需要在左侧、顶部和右侧有边框,但在底部不需要。
回答by Android Stack
Create an XML file named border.xml in the drawable folder and put the following code in it.
在 drawable 文件夹中创建一个名为 border.xml 的 XML 文件,并将以下代码放入其中。
<?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="#FF0000" />
</shape>
</item>
<item android:left="5dp" android:right="5dp" android:top="5dp" >
<shape android:shape="rectangle">
<solid android:color="#000000" />
</shape>
</item>
</layer-list>
Then add a background to your linear layout like this:
然后为您的线性布局添加背景,如下所示:
android:background="@drawable/border"
EDIT :
编辑 :
This XML was tested with a galaxy s running GingerBread 2.3.3and ran perfectly as shown in image below:
此 XML 已使用运行GingerBread 2.3.3的Galaxys 进行测试,并完美运行,如下图所示:
ALSO
还
tested with galaxy s 3 running JellyBean 4.1.2and ran perfectly as shown in image below :
使用运行JellyBean 4.1.2 的 Galaxys 3 进行测试并完美运行,如下图所示:
Finally its works perfectly with all APIs
最后它与所有 API 完美配合
EDIT 2 :
编辑 2:
It can also be done using a stroke to keep the background as transparent while still keeping a border except at the bottom with the following code.
也可以使用描边来完成,以保持背景透明,同时仍然保留边框,除了在底部使用以下代码。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:left="0dp" android:right="0dp" android:top="0dp"
android:bottom="-10dp">
<shape android:shape="rectangle">
<stroke android:width="10dp" android:color="#B22222" />
</shape>
</item>
</layer-list>
hope this help .
希望这有帮助。
回答by Kenny
Save this xml and add as a background for the linear layout....
保存此 xml 并添加为线性布局的背景....
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="4dp" android:color="#FF00FF00" />
<solid android:color="#ffffff" />
<padding android:left="7dp" android:top="7dp"
android:right="7dp" android:bottom="0dp" />
<corners android:radius="4dp" />
</shape>
Hope this helps! :)
希望这可以帮助!:)
回答by Jonas
Kenny is right, just want to clear some things out.
肯尼是对的,只是想澄清一些事情。
- Create the file
border.xml
and put it in the folderres/drawable/
add the code
<shape xmlns:android="http://schemas.android.com/apk/res/android"> <stroke android:width="4dp" android:color="#FF00FF00" /> <solid android:color="#ffffff" /> <padding android:left="7dp" android:top="7dp" android:right="7dp" android:bottom="0dp" /> <corners android:radius="4dp" /> </shape>
set back ground like
android:background="@drawable/border"
wherever you want the border
- 创建文件
border.xml
并将其放入文件夹res/drawable/
添加代码
<shape xmlns:android="http://schemas.android.com/apk/res/android"> <stroke android:width="4dp" android:color="#FF00FF00" /> <solid android:color="#ffffff" /> <padding android:left="7dp" android:top="7dp" android:right="7dp" android:bottom="0dp" /> <corners android:radius="4dp" /> </shape>
像
android:background="@drawable/border"
你想要边界的任何地方一样设置背景
Mine first didn't work cause i put the border.xml
in the wrong folder!
我的第一个不起作用,因为我把它border.xml
放在错误的文件夹中!
回答by PAD
Here is a Github linkto a lightweight and very easy to integrate library that enables you to play with borders as you want for any widget you want, simply based on a FrameLayout widget.
这是一个指向轻量级且非常易于集成的库的Github 链接,它使您可以根据需要为任何您想要的小部件使用边框,只需基于 FrameLayout 小部件。
Here is a quick sample code for you to see how easy it is, but you will find more information on the link.
这是一个快速示例代码,您可以看到它有多简单,但您可以在链接上找到更多信息。
<com.khandelwal.library.view.BorderFrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:leftBorderColor="#00F0F0"
app:leftBorderWidth="10dp"
app:topBorderColor="#F0F000"
app:topBorderWidth="15dp"
app:rightBorderColor="#F000F0"
app:rightBorderWidth="20dp"
app:bottomBorderColor="#000000"
app:bottomBorderWidth="25dp" >
</com.khandelwal.library.view.BorderFrameLayout>
So, if you don't want borders on bottom, delete the two lines about bottom in this custom widget, and that's done.
因此,如果您不希望底部有边框,请删除此自定义小部件中关于底部的两行,然后就完成了。
And no, I'm neither the author of this library nor one of his friend ;-)
不,我既不是这个图书馆的作者,也不是他的朋友之一 ;-)