Android自定义shape的使用示例

 更新时间:2013年06月24日 17:34:40   作者:  
本文主要介绍自定义shape(定义矩形、定义边框颜色、定义圆角弧度),具体代码如下,感兴趣的各位可以参考下哈,希望对大家有所帮助
MainActivity如下:
复制代码 代码如下:

package cn.testshape;
import android.os.Bundle;
import android.app.Activity;
/**
* Demo描述:
* 自定义shape的使用
*/
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}

main.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"
>
<Button
android:layout_width="250dip"
android:layout_height="50dip"
android:text="测试自定义shape的使用"
android:background="@drawable/background_selector"
android:textColor="@drawable/textcolor_selector"
android:layout_centerInParent="true"
android:gravity="center"
/>
</RelativeLayout>

background_selector.xml如下:
复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/pressed_shape" android:state_pressed="true"/>
<item android:drawable="@drawable/default_shape"/>
</selector>

default_shape.xml如下:
复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<!-- 定义矩形rectangle -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- 定义边框颜色 -->
<solid android:color="#d1d1d1" />
<!-- 定义圆角弧度 -->
<corners
android:bottomLeftRadius="4dp"
android:bottomRightRadius="4dp"
android:topLeftRadius="4dp"
android:topRightRadius="4dp"
/>
</shape>

pressed_shape.xml如下:
复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<!-- 定义矩形rectangle -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- 定义边框颜色 -->
<solid android:color="#7bb3f8" />
<!-- 定义圆角弧度 -->
<corners
android:bottomLeftRadius="4dp"
android:bottomRightRadius="4dp"
android:topLeftRadius="4dp"
android:topRightRadius="4dp"
/>
</shape>

textcolor_selector.xml如下:
复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:color="#ffffff" android:state_pressed="true"/>
<item android:color="#000000"/>
</selector>

相关文章

  • Material Design系列之Behavior实现Android知乎首页

    Material Design系列之Behavior实现Android知乎首页

    这篇文章主要为大家详细介绍了Material Design系列之Behavior实现Android知乎首页的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • Android代码实现AdapterViews和RecyclerView无限滚动

    Android代码实现AdapterViews和RecyclerView无限滚动

    这篇文章主要为大家详细介绍了Android代码实现AdapterViews和RecyclerView无限滚动的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-07-07
  • Android入门之IntentService的使用教程详解

    Android入门之IntentService的使用教程详解

    IntentService的生命周期中有一个非常好的方法-onHandleIntent方法,它是一个abstract方法,开发者在实现IntentService时可以覆盖它来处理“长事务”。本文就来聊聊IntentService的使用,需要的可以参考一下
    2022-12-12
  • Android中的导航navigation的使用详细步骤

    Android中的导航navigation的使用详细步骤

    在Android中,导航主要通过使用Navigation SDK来实现,该SDK提供了一组工具和组件,可以帮助开发人员构建具有一致性和可访问性的用户界面,这篇文章主要介绍了Android中的导航navigation的使用详细步骤,需要的朋友可以参考下
    2024-04-04
  • Android实现将View转化为图片并保存到本地

    Android实现将View转化为图片并保存到本地

    这篇文章主要为大家详细介绍了Android实现将View转化为图片并保存到本地,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • Android Mms之:联系人管理的应用分析

    Android Mms之:联系人管理的应用分析

    本篇文章是对Android中的联系人管理进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • Android开启闪光灯的方法 Android打开手电筒功能

    Android开启闪光灯的方法 Android打开手电筒功能

    这篇文章主要为大家详细介绍了Android开启闪光灯的方法,Android打开手电筒功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • Android实现顶部导航菜单左右滑动效果

    Android实现顶部导航菜单左右滑动效果

    这篇文章主要为大家详细介绍了Android实现顶部导航菜单左右滑动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-06-06
  • CDC与BG-CDC的含义电容触控学习整理

    CDC与BG-CDC的含义电容触控学习整理

    今天小编就为大家分享一篇关于CDC与BG-CDC的含义电容触控学习整理,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • Android自定义PhotoView使用教程

    Android自定义PhotoView使用教程

    自定义 PhotoView 继承(extends)自 View。并在最中间显示后面操作的图片。绘制图片可以重写 onDraw()方法,并在里面通过Canvas.drawBitmap()来要绘制图片
    2023-04-04

最新评论