使用shape为android各种元素制作圆角

来源:互联网 发布:自动刷火车票软件 编辑:程序博客网 时间:2024/06/16 08:28

1.Shape

简介

作用:XML中定义的几何形状

位置:res/drawable/文件的名称.xml

使用的方法:

Java代码中:R.drawable.文件的名称

XML中:android:background="@drawable/文件的名称"

属性:

shape 有如下属性

其中

android:shape=["rectangle" | "oval" | "line" | "ring"]  

rectagle矩形,oval椭圆,line水平直线,ring环形 



<shape>中子节点的常用属性: 
 
<gradient>  渐变 
 
android:startColor  起始颜色 
 
android:endColor  结束颜色              
 
android:angle  渐变角度,0从上到下,90表示从左到右,数值为45的整数倍默认为0; 
 
android:type  渐变的样式 liner线性渐变 radial环形渐变 sweep 
 
<solid >  填充 
 
android:color  填充的颜色 
 
<stroke > 描边 
 
android:width 描边的宽度 
 
android:color 描边的颜色 
 
android:dashWidth 表示'-'横线的宽度 
 
android:dashGap 表示'-'横线之间的距离 
 
<corners > 圆角 
 
android:radius  圆角的半径 值越大角越圆 
 
android:topRightRadius  右上圆角半径 
   
android:bottomLeftRadius 右下圆角角半径 
  
android:topLeftRadius 左上圆角半径 
 
android:bottomRightRadius 左下圆角半径 


2. 使用shape给android元素(Layout, TextView,Button,ImageView)制作圆角


首先制作自己的shape


<?xml version="1.0" encoding="utf-8"?>
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 若使用 android:radius 则四角均射同样的圆角-->
    <corners
        android:radius="10dp" />


    <gradient
        android:endColor="#f8ff5f"
        android:startColor="#f8ff5f" />


    <stroke
        android:width="2px"
        android:color="@android:color/black" />


</shape>


然后我们可以将自己制作的shape放入所需要的元素的background中,见下面的xml布局:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.testandroidshape.MainActivity" >


    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/abc_search_url_text_selected" >


        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="30dip"
            android:background="@drawable/my_shape"
            android:text="@string/hello_world"
            android:textSize="30sp" />


        <ImageView
            android:layout_marginTop="30dip"
            android:id="@+id/image"
            android:layout_width="100dip"
            android:layout_height="50dip"
            android:layout_below="@id/text"
            android:layout_centerHorizontal="true"
            android:background="@drawable/my_shape"
            android:src="@drawable/bg_individual_center_new_login" />


        <Button
            android:layout_marginTop="30dip"
            android:layout_width="100dip"
            android:layout_height="50dip"
            android:layout_below="@id/image"
            android:layout_centerHorizontal="true"
            android:background="@drawable/my_shape"
            android:text="button" />
    </RelativeLayout>


</RelativeLayout>

效果图:


可以发现:除imageView外,layout、button、textview(与button类似)均能使用这种方式添加圆角。

至于imageview添加圆角的方式,我将单独写一篇文章说明


0 0
原创粉丝点击