Android Shape 的使用

这篇具有很好参考价值的文章主要介绍了Android Shape 的使用。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

目录

什么是Shape?

shape属性

子标签属性

corners (圆角)

solid (填充色)

gradient (渐变)

stroke (描边)

padding (内边距)

size (大小)

特殊属性

rectangle(矩形)

oval(椭圆)

line(线)

ring(圆环)

shape 用法


什么是Shape?

在Android开发中,我们可以使用shape定义各种各样的形状,也可以定义一些图片资源。相对于传统图片来说,使用shape可以减少资源占用,减少安装包大小,还能够很好地适配不同尺寸的手机。

shape属性

shape属性的基本语法示例

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape=["rectangle" | "oval" | "line" | "ring"] > // 定义形状

    <corners //圆角属性
        android:radius="integer"
        android:topLeftRadius="integer"
        android:topRightRadius="integer"
        android:bottomLeftRadius="integer"
        android:bottomRightRadius="integer" />

    <gradient //渐变属性
        android:angle="integer"
        android:centerX="integer"
        android:centerY="integer"
        android:centerColor="integer"
        android:endColor="color"
        android:gradientRadius="integer"
        android:startColor="color"
        android:type=["linear" | "radial" | "sweep"]
        android:useLevel=["true" | "false"] />

    <padding //边距属性
        android:left="integer"
        android:top="integer"
        android:right="integer"
        android:bottom="integer" />

    <size //大小属性
        android:width="integer"
        android:height="integer" />

    <solid //填充属性
        android:color="color" />

    <stroke //描边属性
        android:width="integer"
        android:color="color"
        android:dashWidth="integer"
        android:dashGap="integer" />

</shape>

子标签属性

Shape可以定义控件的一些展示效果,例如圆角,渐变,填充,描边,大小,边距; shape 子标签就可以实现这些效果,shape 子标签有下面几个属性:corners,gradient,padding,size,solid,stroke

corners (圆角)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <corners //定义圆角
        android:radius="10dp" //全部的圆角半径;
        android:topLeftRadius="5dp" //左上角的圆角半径;
        android:topRightRadius="5dp" //右上角的圆角半径;
        android:bottomLeftRadius="5dp" //左下角的圆角半径;
        android:bottomRightRadius="5dp" /> //右下角的圆角半径。
</shape>

solid (填充色)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#ffff00"/> //内部填充色
</shape>

gradient (渐变)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <gradient
        android:type=["linear" | "radial" | "sweep"] //共有3中渐变类型,线性渐变(默认)/放射渐变/扫描式渐变;
        android:angle="90" //渐变角度,必须为45的倍数,0为从左到右,90为从上到下;
        android:centerX="0.5" //渐变中心X的相当位置,范围为0~1;
        android:centerY="0.5" //渐变中心Y的相当位置,范围为0~1;
        android:startColor="#24e9f2" //渐变开始点的颜色;
        android:centerColor="#2564ef" //渐变中间点的颜色,在开始与结束点之间;
        android:endColor="#25f1ef" //渐变结束点的颜色;
        android:gradientRadius="5dp" //渐变的半径,只有当渐变类型为radial时才能使用;
        android:useLevel="false" /> //使用LevelListDrawable时就要设置为true。设为false时才有渐变效果。
</shape>

stroke (描边)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <stroke
        android:width="1dp" //描边的宽度
        android:color="#ff0000" //描边的颜色
        // 以下两个属性设置虚线
        android:dashWidth="1dp" //虚线的宽度,值为0时是实线
        android:dashGap="1dp" />//虚线的间隔
</shape>

padding (内边距)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <padding
        android:left="10dp" //左内边距;
        android:top="10dp" //上内边距;
        android:right="10dp" //右内边距;
        android:bottom="10dp" /> //下内边距。
</shape>

size (大小)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <size
        android:width="50dp" //宽度
        android:height="50dp" />// 高度
</shape>

特殊属性

Shape可以定义当前Shape的形状的,比如矩形,椭圆形,线形和环形;这些都是通过 shape 标签属性来定义的, shape 标签有下面几个属性:rectangle,oval,line,ring

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape=["rectangle" | "oval" | "line" | "ring"] //shape的形状,默认为矩形,可以设置为矩形(rectangle)、椭圆形(oval)、线性形状(line)、环形(ring)
    //下面的属性只有在android:shape="ring"时可用:
    android:innerRadius="10dp" // 内环的半径;
    android:innerRadiusRatio="2" // 浮点型,以环的宽度比率来表示内环的半径;
    android:thickness="3dp" // 环的厚度;
    android:thicknessRatio="2" // 浮点型,以环的宽度比率来表示环的厚度;
    android:useLevel="false"> // boolean值,如果当做是LevelListDrawable使用时值为true,否则为false。
</shape>

rectangle(矩形)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/colorPrimary"/>
</shape>

oval(椭圆)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="@color/colorPrimary"/>
    <size android:height="100dp"
        android:width="100dp"/>
</shape>

line(线)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
    <stroke
        android:width="1dp"
        android:color="@color/colorAccent"
        android:dashGap="3dp"//虚线间距
        android:dashWidth="4dp"/>//虚线宽度
    <size android:height="3dp"/>
</shape>

ring(圆环)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:useLevel="false"
    android:innerRadius="20dp" // 内环的半径
    android:thickness="10dp"> // 圆环宽度
    <!--useLevel需要设置为false-->
    <solid android:color="@color/colorAccent"/>
</shape>

shape 用法

1. 在res/drawable下新建 shape_text.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
		android:shape="rectangle">

	<corners
			android:radius="5dp"
			android:topLeftRadius="15dp"
			android:topRightRadius="15dp"
			android:bottomLeftRadius="15dp"
			android:bottomRightRadius="15dp" />

	<gradient
			android:startColor="#FF0000"
			android:endColor="#80FF00"
			android:angle="45" />

	<padding
			android:left="10dp"
			android:top="10dp"
			android:right="10dp"
			android:bottom="10dp" />

	<size
			android:width="200dp"
			android:height="200dp" />

	<solid android:color="#ffff9d" />

	<stroke
			android:width="2dp"
			android:color="#dcdcdc" />
</shape>

2. 在布局中引用 shape_text.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Shape测试"
        android:background="@drawable/shape_text"
        android:textSize="15sp"
        android:textColor="@android:color/black"/>
</LinearLayout>

shape 使用示例

在src-main-res-drawable下,右键 New-Drawable Resource File

Android Shape 的使用,Android,android

Android Shape 的使用,Android,android

会生成一个这样的文件

Android Shape 的使用,Android,android

然后在上面代码中找一个示例

Android Shape 的使用,Android,android

然后在我们的 layout 文件中使用 shape,使用效果图如下

Android Shape 的使用,Android,android

 文章来源地址https://www.toymoban.com/news/detail-651263.html

到了这里,关于Android Shape 的使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【Android】使用Android Studio打包APK文件

    打包APK之前,首先需要新建项目,有基础的可以跳过。 无基础的可以参考:使用Android Studio运行Hello World项目 1.找到Build - Generate Signed Bundle or APK - 勾选APK 2.首次需要创建证书,后续可以使用已创建好的证书 3.填写证书信息 选择密钥库存放路径,并填写密码 填写密钥名称、密

    2024年02月19日
    浏览(42)
  • 【Android】最新版Android13使用Notification,Notification的基本使用和进阶使用

    1.1 注册一个渠道 在Android13,版本通知的使用发生了新的变化。 1.1.1 NotificationManager原生类 首先我们需要创建一个 NotificationManager 用于管理通知。 NotificationManager 仅支持在 API 等级 11(Android 3.0)及以上的设备上使用 ,因此在较旧的 Android 版本上无法使用较新的通知功能。 `

    2024年01月17日
    浏览(31)
  • Android GreenDao 使用全面讲解,Android组件化入门

    5. 在多个线程中使用QueryBuilder 如果在多个线程中使用查询,则必须调用 forCurrentThread ()以获取当前线程的Query实例。Query的对象实例绑定到构建查询的拥有线程。 这使您可以安全地在Query对象上设置参数,而其他线程不会干扰。如果其他线程尝试在查询上设置参数或执行绑

    2024年04月27日
    浏览(42)
  • Android查看签名信息系列 · 使用Android Studio获取签名

    前言 Android查看签名信息系列 之使用Android Studio获取签名,通过Android Studio自带的gradle来获取签名信息。 优点:此法可查看 MD5、SHA1 等信息。 缺点:升级某个Studio版本后,没有签名任务了,特别不方便。 实现方法 一、使用 Android Studio 创建gradle获取签名信息。 1、使用 Androi

    2024年02月07日
    浏览(36)
  • 【Android笔记97】Android之RecyclerView使用GridLayoutManager网格布局

    这篇文章,主要介绍Android之RecyclerView使用GridLayoutManager网格布局。 目录 一、GridLayoutManager网格布局 1.1、功能效果 1.2、案例代码 (1)创建网格布局

    2024年02月15日
    浏览(28)
  • 【Android笔记108】Android之翻转视图组件ViewFlipper的使用

    这篇文章,主要介绍Android之翻转视图组件ViewFlipper的使用。 目录 一、翻转视图ViewFlipper 1.1、什么是ViewFlipper 1.2、运行效果 (1)不带动画的效果

    2024年02月08日
    浏览(47)
  • Android Architecture Components ——LiveData使用,android热修复视频

    小结:getApplication()即是全局的上下文 Activity中进行数据监听 import android.arch.lifecycle.MutableLiveData; import android.arch.lifecycle.Observer; import android.arch.lifecycle.ViewModelProviders; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log;

    2024年04月17日
    浏览(32)
  • Android串口开发之使用JNI实现ANDROID和串口通信

    导语:Android串口通信在物联网、智能家居等领域具有广泛的应用。本文将详细介绍如何使用JNI技术实现Android设备与串口的通信,包括串口的打开、设置参数和读写数据等过程。 在开始介绍Android串口开发之前,我们需要了解以下几个概念: JNI:JNI(Java Native Interface)是一种

    2024年02月07日
    浏览(39)
  • Android、Android Studio 和 Gradle 是如何使用 Java 的

    可以肯定地说,每个人都知道 Android 使用 Java。然而,“使用 Java ”的含义范围相当大。因此,在本文中,我将介绍 Android 设备和我们用于 Android 开发的各种工具是如何使用 Java 平台的。 让我们从结论开始——下图显示了 Java 在 Android 生态系统中的作用: 使用 Java 编程语言编

    2024年02月10日
    浏览(34)
  • [Android]Mac电脑Android Studio使用真机调试运行

    我这里是一台中兴手机 1. 手机打开USB调试 打开“设置”找到“关于手机”进入,连续点击版本号,直到提示“您已经进入开发者模式”。 回到“设置”找到“系统与更新”进入,再进入“开发者选项”,打开USB调试。 2. 用数据线连接电脑和手机 USB的用途选择“文件传输”

    2023年04月08日
    浏览(34)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包