android - 如何在除底部之外的线性布局周围添加边框?

标签 android android-linearlayout

除了底部之外,如何在线性布局周围添加边框? 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 上进行了测试,并且运行良好,如下图所示:

enter image description here

在运行 JellyBean 4.1.2 的 Galaxy s 3 上进行了测试,并完美运行,如下图所示:

enter image description here

终于它与所有 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/

相关文章:

android - 如何创建具有半透明背景颜色和圆角半径的 CardView?

android - 在 listView onItemSelected Android 中更改 ImageView 背景

java - com/android/dx/command/dexer/Main : Unsupported major. 次要版本 52.0

android - Android 中线性布局中按钮的右对齐,按钮并排

Android ArrayAdapter/ListView 不会在 Fragment 中更新

android - scrollview 在 android 中太慢

java - 是转换整个表达式更好,还是只转换不同类型的变量更好

Android:让 Recyclerview 表现得像 LinearLayout

android - 如何使用 XML 布局以外的方式绘制自定义 View 装饰?

java - 在 Android 中显示图像上的关键点 - OpenCV