除了底部之外,如何在线性布局周围添加边框? LinearLayout 需要在左侧、顶部和右侧有边框,但不能在底部。
最佳答案
在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>
然后像这样为您的线性布局添加背景:
android:background="@drawable/border"
编辑:
此 XML 已在运行 GingerBread 2.3.3 的 Galaxy s 上进行了测试,并且运行良好,如下图所示:
也
在运行 JellyBean 4.1.2 的 Galaxy s 3 上进行了测试,并完美运行,如下图所示:
终于它与所有 API 完美配合
编辑 2:
也可以使用描边来保持背景透明,同时除了底部之外仍然保持边框,使用以下代码。
<?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>
希望对您有所帮助。
关于android - 如何在除底部之外的线性布局周围添加边框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10457135/