android 使用shape做有边框背景的方法搜集整理

方法 1

 xmlns:android="http://schemas.android.com/apk/res/android">


      
             android:color="#535353" />
      


 android:bottom="1dp">
     
            android:color="#252525" />
     

方法2 

 xmlns:android="http://schemas.android.com/apk/res/android">
    
    
          
                 android:left="0dp" android:top="1dp" android:right="0dp" android:bottom="1dp"/>
                 android:color="#898989" />
          
    
    
    
         
              android:color="#ffffff" />
         
    

方法3 ,在高分屏下可能会有问题,解决方法是把-1dp换成-1px。最好的方法是为不同密度屏幕设置不同的密度资源文件【dimension resources】dimens.xml

xml version="1.0" encoding="utf-8"?>

 xmlns:android="http://schemas.android.com/apk/res/android">

     android:top="-1dp" android:right="-1dp" android:left="-1dp">
      
             android:color="@android:color/transparent" />
             android:width="1dp" android:color="#ffffff" />
      
    

方法4,这应该是hack, angle 0=left 90=bottom 180=right 270=top

xml version="1.0" encoding="utf-8"?>
 xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    
        android:angle="0"
        android:startColor="#f00"
        android:centerColor="@android:color/transparent"
        android:centerX="0.01" />

方法5

xml version="1.0" encoding="utf-8"?>
 xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetTop="-2dp" 
    android:insetBottom="-2dp"
    android:insetLeft="-2dp">

     android:shape="rectangle">
         android:width="2dp" android:color="#FF0000" />
         android:color="#000000" />
    



方法6

1: A border.xml shape, which is just a solid shape in the color of your border: border.xml

xml version="1.0" encoding="UTF-8"?>
 xmlns:android="http://schemas.android.com/apk/res/android">
     android:color="#ff0000"/>

2: The 'inner' shape, the shape where you want the border to appear around: inner.xml

xml version="1.0" encoding="UTF-8"?>
 xmlns:android="http://schemas.android.com/apk/res/android">
     android:color="#00ff00"/>

3: A layer list, which will put these 2 on top of eachother. You create the border by setting the padding on the inner shape: layerlist.xml

xml version="1.0" encoding="UTF-8"?>
 xmlns:android="http://schemas.android.com/apk/res/android">
 android:drawable="@drawable/border"/>
 android:drawable="@drawable/inner"
    android:top="3dp" android:right="0dp" android:bottom="3dp"
    android:left="3dp" />

方法7  最长常用的方法,就是使用9-patch文件,这个不属于shape


总结:方法 1 3 6 原理应该大致相同

你可能感兴趣的:(Android)