Android布局实现圆角边框的两个方法

方法一 使用 drawable-mdpi

设置边框圆角可以在drawable-mdpi目录里定义一个xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">  
    <solid android:color="#000000" />  
    <corners android:topLeftRadius="10dp"
                    android:topRightRadius="10dp"  
                android:bottomRightRadius="10dp"
                android:bottomLeftRadius="10dp"/>  
</shape>

解释:solid的表示填充颜色,为了简单,这里用的是黑色。
而corners则是表示圆角,注意的是这里bottomRightRadius是左下角而不是右下角,bottomLeftRadius右下角。
当然上面的效果也可以像下面一样设置,如下:

<corners android:radius="5dp" />

如果想引用这个xml,只需要@drawable/corners_bg.xml即可:

android:background="@drawable/corners_bg"

最后main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:background="#FFFFFF">
    <RelativeLayout android:id="@+id/login_div"
        android:layout_width="fill_parent" android:layout_height="150dip"
        android:padding="15dip" android:layout_margin="15dip"
        android:background="@drawable/corners_bg">
    </RelativeLayout>
</LinearLayout>

方法二 Android使用 Shape 画边框线

 

1、布局

    <?xml version="1.0" encoding="utf-8"?>    
    <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"    
        android:layout_width="fill_parent"    
        android:layout_height="fill_parent"    
        android:background="#FFFFFF"    
        android:orientation="vertical" >    
         
        <!-- 表格布局 -->    
        <TableLayout    
            android:layout_width="fill_parent"    
            android:layout_height="wrap_content"    
            android:padding="10dip" >    
            <!-- 表格布局:第一行 -->    
            <TableRow    
                android:layout_width="fill_parent"    
                android:layout_height="wrap_content"    
                android:background="@drawable/shape_top_corner_no_bottom_line"    
                android:padding="10dip" >    
                <TextView    
                    android:layout_width="wrap_content"    
                    android:layout_height="wrap_content"    
                    android:layout_gravity="center_vertical"    
                    android:layout_marginRight="10dip"    
                    android:text="姓名:" >    
                </TextView>    
                <EditText    
                    android:id="@+id/bankingYourNameEditText"    
                    android:layout_width="wrap_content"    
                    android:layout_height="wrap_content"    
                    android:layout_gravity="center_vertical"    
                    android:layout_weight="1"    
                    android:background="@null"    
                    android:singleLine="true" >    
                </EditText>    
            </TableRow>    
            <!-- 表格布局:第二行 -->    
            <TableRow    
                android:layout_width="fill_parent"    
                android:layout_height="wrap_content"    
                android:background="@drawable/shape_no_corner_without_bottom"    
                android:padding="10dip" >    
                <TextView    
                    android:layout_width="wrap_content"    
                    android:layout_height="wrap_content"    
                    android:layout_gravity="center_vertical"    
                    android:layout_marginRight="10dip"    
                    android:text="联系电话:" >    
                </TextView>    
                <EditText    
                    android:id="@+id/bankingContactTelEditText"    
                    android:layout_width="wrap_content"    
                    android:layout_height="wrap_content"    
                    android:layout_gravity="center_vertical"    
                    android:layout_weight="1"    
                    android:background="@null"    
                    android:inputType="phone"    
                    android:singleLine="true" >    
                </EditText>    
            </TableRow>    
            <!-- 表格布局:第三行 -->    
            <TableRow    
                android:layout_width="fill_parent"    
                android:layout_height="wrap_content"    
                android:background="@drawable/shape_bottom_corner_no_top_line"    
                android:padding="10dip" >    
                <TextView    
                    android:layout_width="wrap_content"    
                    android:layout_height="wrap_content"    
                    android:layout_gravity="center_vertical"    
                    android:layout_marginRight="10dip"    
                    android:text="联系电话:" >    
                </TextView>    
                <EditText    
                    android:id="@+id/bankingContactTelEditText"    
                    android:layout_width="wrap_content"    
                    android:layout_height="wrap_content"    
                    android:layout_gravity="center_vertical"    
                    android:layout_weight="1"    
                    android:background="@null"    
                    android:inputType="phone"    
                    android:singleLine="true" >    
                </EditText>    
            </TableRow>    
        </TableLayout>    
         
        <Button    
            android:id="@+id/button1"    
            android:layout_width="wrap_content"    
            android:layout_height="wrap_content"    
            android:layout_gravity="center"    
            android:text="Button" />    
         
    </LinearLayout>    

 

 

2、表格布局中每个TableRow表示一行,TableRow中的每个基本控件都是一列,这是一个三行两列的布局

这里的表格背景是自定义的shape,下面分别看一下三个shape的代码。

shape_top_corner_no_bottom_line.xml文件:顶部带圆角 白色背景 灰色边框 无下边框 长方体

    <?xml version="1.0" encoding="UTF-8"?>    
    <!-- 顶部带圆角 白色背景 灰色边框 无下边框 长方体 -->    
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">    
        <item>    
            <shape>    
                <solid android:color="#FFFFFF" />    
                <corners android:topLeftRadius="10dp" android:topRightRadius="10dp"    
                    android:bottomRightRadius="0.1dp" android:bottomLeftRadius="0.1dp" />    
                <stroke android:width="1dp" android:color="#ffa8abad" />    
            </shape>    
        </item>    
        <item android:top="1dp" android:left="1dp" android:right="1dp">    
            <shape>    
                <solid android:color="#FFFFFF" />    
                <corners android:topLeftRadius="10dp" android:topRightRadius="10dp"    
                    android:bottomRightRadius="0.1dp" android:bottomLeftRadius="0.1dp" />    
                <stroke android:width="1dp" android:color="#ffffffff" />    
            </shape>    
        </item>    
    </layer-list>    

 

3、shape_no_corner_without_bottom.xml文件:不带圆角 白色背景 灰色边框 无下边框 长方体

    <?xml version="1.0" encoding="UTF-8"?>    
    <!-- 不带圆角 白色背景 灰色边框 无下边框 长方体 -->    
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >    
        <item>    
            <shape>    
                <solid android:color="#FFFFFF" />    
                <stroke    
                    android:width="1dp"    
                    android:color="#ffa8abad" />    
            </shape>    
        </item>    
        <item    
            android:left="1dp"    
            android:right="1dp"    
            android:top="1dp">    
            <shape>    
                <solid android:color="#FFFFFF" />    
                <stroke    
                    android:width="1dp"    
                    android:color="#ffffffff" />    
            </shape>    
        </item>    
    </layer-list>    

 

4、shape_bottom_corner_no_top_line.xml文件:底部圆角 白色背景 灰色边框 长方体

    <?xml version="1.0" encoding="UTF-8"?>    
    <!-- 底部圆角 白色背景 灰色边框 长方体 -->    
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">    
        <item>    
            <shape>    
                <solid android:color="#FFFFFF" />    
                <corners android:topLeftRadius="0.1dp" android:topRightRadius="0.1dp"    
                    android:bottomRightRadius="10dp" android:bottomLeftRadius="10dp" />    
                <stroke android:width="1dp" android:color="#ffa8abad" />    
            </shape>    
        </item>    
        <item android:top="1dp" android:bottom="1dp" android:left="1dp" android:right="1dp">    
            <shape>    
                <solid android:color="#FFFFFF" />    
                <corners android:topLeftRadius="0.1dp" android:topRightRadius="0.1dp"    
                    android:bottomRightRadius="10dp" android:bottomLeftRadius="10dp" />    
                <stroke android:width="1dp" android:color="#ffffffff" />    
            </shape>    
        </item>    
    </layer-list>   

 

5、说明:

     shape_top_corner_no_bottom_line.xml

     shape_no_corner_without_bottom.xml

     shape_bottom_corner_no_top_line.xml

     以上三个文件都存放在 drawable 中。

 

6、效果图查看附件。

时间: 2024-07-30 17:58:07

Android布局实现圆角边框的两个方法的相关文章

Android布局实现圆角边框效果_Android

首先,在res下面新建一个文件夹drawable,在drawable下面新建三个xml文件:shape_corner_down.xml.shape_corner_up.xml和shape_corner.xml,分别是下面两个角是圆角边框,上面两个角是圆角边框,四个角全部是圆角边框.  shape_corner_down.xml: <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android=

Android布局实现圆角边框效果

首先,在res下面新建一个文件夹drawable,在drawable下面新建三个xml文件:shape_corner_down.xml.shape_corner_up.xml和shape_corner.xml,分别是下面两个角是圆角边框,上面两个角是圆角边框,四个角全部是圆角边框. shape_corner_down.xml: <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android=&

Android布局实现圆角边框

这里用的是TableLayout布局的.先看效果图 下面看下布局文件 [html] view plaincopy <?xml version="1.0" encoding="utf-8"?>   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"       android:layout_width="fill_parent&

Android编程实现圆角边框的方法_Android

本文实例讲述了Android编程实现圆角边框的方法.分享给大家供大家参考,具体如下: 设置边框圆角可以在drawable-mdpi目录里定义一个xml: <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="

Android布局之LinearLayout自定义高亮背景的方法

本文实例讲述了Android布局之LinearLayout自定义高亮背景的方法.分享给大家供大家参考,具体如下: 首先创建linearlayout_background.xml文件 res/drawable/linearlayout_background.xml <?xml version="1.0" encoding="utf-8"?> <selectorxmlns:android="http://schemas.android.com

Android中Intent传递对象的两种方法(Serializable,Parcelable)

Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是Bundle.putParcelable(Key, Object);当然这些Object是有一定的条件的,前者是实现了Serializable接口,而后者是实现了Parcelable接口,为了让大家更容易理解我还是照常写了一个简单的Demo,大家就一步一步跟我来吧! 第一步:新建一个Android工程命名为ObjectTranDemo(类比较

Android中实现圆角图片的几种方法

Android中实现圆角图片有多种姿势,不知你解锁了几种? 方法一:setXfermode法 此种方式就是再new一个相同尺寸的bitmap,然后使用paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));先画圆角矩形,再画原始bitmap,然后就得到了一个圆角的bitmap了. public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) { Bitmap

Android通过LIstView显示文件列表的两种方法介绍_Android

在Android中通过ListView显示SD卡中的文件列表一共有两种方法,一是:通过继承ListActivity显示;二是:利用BaseAdapter显示.BaseAdapter是一个公共基类适配器,用于对ListView和Spinner等 一些控件提供显示数据.下面是利用BaseAdapter类来实现通过LIstView显示SD卡的步骤: 1.main.xml界面设计,如下图 复制代码 代码如下: <?xml version="1.0" encoding="utf-

Android中Intent传递对象的两种方法Serializable,Parcelable

Android中的传递有两个方法,一个是Serializable,另一个是Parcelable. Serializable是J2SE本身就支持的.而Parcelable是Android所特有的. 二者的使用场景和区别: 1)在使用内存的时候,Parcelable比Serializable性能高,所以推荐使用Parcelable. 2)Serializable在序列化的时候会产生大量的临时变量,从而引起频繁的GC. 3)Parcelable不能使用在要将数据存储在磁盘上的情况,因为Parcelab