Andriod开发————圆角边框与圆角背景的实现方式

来源:互联网 发布:安卓修改mac地址 编辑:程序博客网 时间:2024/05/17 02:24

本文主要分享圆角边框与圆角背景的实现方式。该方式的实现,需要了解shape的使用,该部分的详细介绍,请阅读博客http://blog.csdn.net/mahoking/article/details/23672271。文中有较详细的介绍。

【转载使用,请注明出处:http://blog.csdn.net/mahoking】
 如下是演示的shape_layout.xml模板。

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.     <!-- 填充色 -->  
  4.     <solid android:color="#CCFF99"/>  
  5.     <!-- 圆角 -->  
  6.     <corners android:radius="10dp"/>  
  7. </shape>  

       为了显示的好看与协调,本案创建了多个shape_*.xml文件,各个shape_*.xml文件只是solid填充色的配置不同,读者可以根据自己的设计与喜好自行搭配。在本文的而最后,会展示相应Demo截图。

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.     <!-- 填充色 -->  
  4.     <solid android:color="#FF9999"/>  
  5.     <!-- 圆角 -->  
  6. <!-- android:radius 设置角的弧度,值越大角越圆-->  
  7.     <corners android:radius="10dp"/>  
  8. </shape>  

        创建Activity(RoundCornerActivity),对应的布局文件为activity_01_round_corner.xml。
RoundCornerActivity

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  *@describe  圆角边框、圆角背景的实现演示 
  3.  *@date 2014-8-24 22:35:49 
  4.  */  
  5. public class RoundCornerActivity extends Activity{  
  6.   
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.activity_01_round_corner);  
  11.           
  12.     }  
  13. }  

activity_01_round_corner.xml

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="20dp"  
  10.         android:layout_marginLeft="15dp"  
  11.         android:layout_marginRight="15dp"  
  12.         android:layout_marginTop="5dp"  
  13.         android:background="@drawable/shape_01_round_corner_textview"  
  14.         android:gravity="center"  
  15.         android:text="圆角背景与边框演示" />  
  16.   
  17.     <LinearLayout  
  18.         android:layout_width="match_parent"  
  19.         android:layout_height="60dp"  
  20.         android:layout_marginLeft="15dp"  
  21.         android:layout_marginRight="15dp"  
  22.         android:layout_marginTop="10dp"  
  23.         android:background="@drawable/shape_01_round_corner_layout" >  
  24.     </LinearLayout>  
  25.   
  26.     <TextView  
  27.         android:layout_width="match_parent"  
  28.         android:layout_height="20dp"  
  29.         android:layout_marginLeft="15dp"  
  30.         android:layout_marginRight="15dp"  
  31.         android:layout_marginTop="5dp"  
  32.         android:background="@drawable/shape_01_round_corner_textview"  
  33.         android:gravity="center"  
  34.         android:text="以下是特效演示" />  
  35.   
  36.     <LinearLayout  
  37.         android:layout_width="match_parent"  
  38.         android:layout_height="wrap_content"  
  39.         android:layout_marginTop="10dp"  
  40.         android:orientation="horizontal" >  
  41.   
  42.         <LinearLayout  
  43.             android:layout_width="match_parent"  
  44.             android:layout_height="wrap_content"  
  45.             android:layout_marginLeft="5dp"  
  46.             android:layout_marginRight="5dp"  
  47.             android:layout_weight="1"  
  48.             android:orientation="vertical" >  
  49.   
  50.             <TextView  
  51.                 android:layout_width="match_parent"  
  52.                 android:layout_height="120dp"  
  53.                 android:background="@drawable/shape_01_round_corner_textview_ma"  
  54.                 android:gravity="center"  
  55.                 android:text="马"  
  56.                 android:textSize="60dp" />  
  57.         </LinearLayout>  
  58.   
  59.         <LinearLayout  
  60.             android:layout_width="match_parent"  
  61.             android:layout_height="wrap_content"  
  62.             android:layout_marginLeft="5dp"  
  63.             android:layout_marginRight="5dp"  
  64.             android:layout_weight="1"  
  65.             android:orientation="vertical" >  
  66.   
  67.             <TextView  
  68.                 android:layout_width="match_parent"  
  69.                 android:layout_height="55dp"  
  70.                 android:background="@drawable/shape_01_round_corner_textview_yi"  
  71.                 android:gravity="center"  
  72.                 android:text="意"  
  73.                 android:textSize="30dp" />  
  74.   
  75.             <TextView  
  76.                 android:layout_width="match_parent"  
  77.                 android:layout_height="55dp"  
  78.                 android:layout_marginTop="10dp"  
  79.                 android:background="@drawable/shape_01_round_corner_textview_ran"  
  80.                 android:gravity="center"  
  81.                 android:text="然"  
  82.                 android:textSize="30dp" />  
  83.         </LinearLayout>  
  84.     </LinearLayout>  
  85.   
  86. </LinearLayout>  

          切忌不要忘记在AndroidManifest.xml中注册该Activity。

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <application  
  2.         android:allowBackup="true"  
  3.         android:icon="@drawable/uisharing_ico"  
  4.         android:label="@string/app_name"  
  5.         android:theme="@style/AppTheme" >  
  6.         <activity  
  7.             android:name="com.mahaochen.app.uisharing.example01.RoundCornerActivity"  
  8.             android:screenOrientation="portrait"   
  9.             android:label="@string/app_name">  
  10.             <intent-filter>  
  11.                 <action android:name="android.intent.action.MAIN" />  
  12.                 <category android:name="android.intent.category.LAUNCHER" />  
  13.             </intent-filter>  
  14.         </activity>  
  15.     </application>  


运行该项目,效果如下:

 


0 0
原创粉丝点击