注:本文由Colin撰写,版权所有!转载请注明原文地址,谢谢合作!

在做Android开发时,我们为了美观,有时候需要使用圆角矩形,或半透明之类的效果,在网页设计中很容易实现。但在Android开发中,要稍微麻烦一点,但实现起来也不算很难。

关于设定背景图片平铺的方法请参考上一篇文章:http://itcolin.com/archives/1153.html

一、首先,需要在drawable-mdpi目录里定义一个xml文件,我命名为frame

编写如下代码,其中corners 中定义每边的圆角弧度,solid为填充的颜色:半透明颜色:#10000000~#90000000 透明深度不一样。(也可以用:#e0000000)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="#20000000" />
<!-- <stroke android:color="#CCCCCC" android:width="1dp" android:dashWidth="5dp" android:dashGap="3dp"/> -->
<stroke android:color="#20000000" android:width="1dp"/>
<corners
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp" />
</shape>

关于背景颜色需要渐变色的话也可以参考以下代码来控制渐变:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#FFF"
android:endColor="#000"
android:angle="45" />
</shape>

二、在layout配置主文件中将需要设定圆角的Layout(这里演式的是RelativeLayout)背景设置为frame即可。

代码如下:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/content"
android:background="@drawable/bitmap">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@drawable/frame"
android:layout_margin="5dp"
android:padding="5dp"
android:layout_marginTop="20dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
</ScrollView>

此时,运行虚拟机看看效果吧。

实现效果如下图:

Demo 下载地址: Android圆角矩形及半透明演式Demo

最新文章

  1. iptables--简单的防火墙
  2. axure设置变量值
  3. SharePoint 2010: Export User Profile Properties to a Text File or Excel using PowerShell
  4. php 生成 Json
  5. slf4j日志系统
  6. Force.com平台基础--前言
  7. Java基础-String 存储机制管理
  8. VMWare EXSi 添加新磁盘时 报错 HostStorageSystem.ComputeDiskPartitionInfo 的处理
  9. WebView重定向新开界面问题-b
  10. JVM ClassLoader加载过程
  11. -------- Rootkit 核心技术——利用 nt!_MDL 突破 KiServiceTable 的只读访问限制 Part II --------
  12. nltk download失败
  13. [转载]完全理解Python迭代对象、迭代器、生成器
  14. C#并行编程(6):线程同步面面观
  15. 微信怎样做SEO
  16. day6:vcp考试
  17. 使用 BEGINCONTENT() 和 ENDCONTENT() 设定 YII 的 LAYOUTS
  18. 结队第二次作业——WordCount进阶需求
  19. swift - UIProgressView的用法
  20. linux inode cheat sheet

热门文章

  1. Circular Queue Implementation Principle
  2. linux系统安装jdk
  3. 安装PhantomJS
  4. POJ 1064 Cable master (二分)
  5. 计算机图形学 - 图形变换(opengl版)
  6. UVA11624Fire!(BFS)
  7. sudo和rpm命令
  8. C# 文件操作大全
  9. Runner站立会议02
  10. nginx配置文件详解( 看着好长,其实不长,看了就知道了,精心整理,有些配置也是没用到呢 )