android给View设置上下左右边框

Posted 张玉宝

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android给View设置上下左右边框相关的知识,希望对你有一定的参考价值。

给View控件设置边框,可以动态设置上下左右.通过布局文件就能搞定

 

1.在drawable文件夹下新建一个shape_main_list_bg.xml文件

 

  1. <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >  
  2.     <!-- This is the main color -->  
  3.     <item>  
  4.         <shape>  
  5.                 <!--    边框颜色 -->  
  6.             <solid android:color="#00FF00"/>  
  7.         </shape>  
  8.     </item>  
  9.     <!-- 给View的上 左  右设置8dp的边框 -->  
  10.     <item android:top="8dp" android:left="8dp" android:right="8dp" >  
  11.         <shape>  
  12.                 <!--     View填充颜色 -->  
  13.             <solid android:color="#FFFFFF" />  
  14.         </shape>  
  15.     </item>  
  16.   
  17. </layer-list>  

 

 


2.给控件设置背景

 

  1. <EditText  
  2.     android:padding="20dp"  
  3.     android:layout_margin="15dp"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="wrap_content"  
  6.     android:hint="请输入Edittext内容"  
  7.     android:background="@drawable/shape_main_list_bg"/>  

 

 

 

3.效果图如下:

  技术分享

 

推荐下自己创建的android QQ群:202928390 欢迎大家的加入.

  


以上是关于android给View设置上下左右边框的主要内容,如果未能解决你的问题,请参考以下文章

怎么给android 设置边框

Android之android:padding和android:layout_margin的区别

Android View 上下左右四种间距的设置方法

android边框能只定义左边框吗

Android 圆角View设置 圆角边框

android 给view添加阴影