Android中的Shape美化

【Android笔记11】Android通过shape、selector修饰和美化UI控件的样式 目录一、Android组件美化1.1、shape(1)如何使用shape(2)添加TextView控件(3)corners圆角设置(4)solid设置背景(5)stroke设置描边(6)padding设置内边距(7)size设置宽高(8)gradient设置渐变色1.2、selector(1)selector选择器(2)selector使用1.3、shape和selector结合使用有时候,Android默认给我们提供的一些UI控件的样式可能不满足业务需求,那怎么办呢???不慌,Android给我们提供了s 阅读详情
     项目中的美化可是很重要的一步,虽然很多时候我们都可以通过图片来完成各种效果,不过但是觉得android给我们提供了那么强大的xml界面编辑功能,不用怪可惜的,今天刚好用到android:shap,也就来给大家说说。。。
先看下面的代码:

先看下面的代码:

<shape>  
    <!-- 实心 -->  
    <solid android:color="#ff9d77"/>  
    <!-- 渐变 -->  
    <gradient  
        android:startColor="#ff8c00"  
        android:endColor="#FFFFFF"  
        android:angle="270" />  
    <!-- 描边 -->  
    <stroke  
        android:width="2dp"  
        android:color="#dcdcdc" />  
    <!-- 圆角 -->  
    <corners  
        android:radius="2dp" />  
    <padding  
        android:left="10dp"  
        android:top="10dp"  
        android:right="10dp"  
        android:bottom="10dp" />  
</shape>  

solid:实心,就是填充的意思
android:color指定填充的颜色

gradient:渐变
android:startColor和android:endColor分别为起始和结束颜色,ndroid:angle是渐变角度,必须为45的整数倍。
另外渐变默认的模式为android:type="linear",即线性渐变,可以指定渐变为径向渐变,android:type="radial",径向渐变需要指定半径android:gradientRadius="50"。

stroke:描边
android:width="2dp" 描边的宽度,android:color 描边的颜色。
我们还可以把描边弄成虚线的形式,设置方式为:
android:dashWidth="5dp"

android:dashGap="3dp"
其中android:dashWidth表示'-'这样一个横线的宽度,android:dashGap表示之间隔开的距离。

corners:圆角
android:radius为角的弧度,值越大角越圆。
我们还可以把四个角设定成不同的角度,方法为:

        android:topRightRadius="20dp"    右上角  
        android:bottomLeftRadius="20dp"    右下角  
        android:topLeftRadius="1dp"    左上角  
        android:bottomRightRadius="0dp"    左下角  



这里有个地方需要注意,bottomLeftRadius是右下角,而不是左下角,这个有点郁闷,不过不影响使用,记得别搞错了就行。
还有网上看到有人说设置成0dp无效,不过我在测试中发现是可以的,我用的是2.2,可能修复了这个问题吧,如果无效的话那就只能设成1dp了。

padding:间隔
这个就不用多说了,XML布局文件中经常用到。


大体的就是这样,以下是一个使用的具体示例:用在Selector中作为Button的背景,分别定义了按钮的一般状态、获得焦点状态和按下时的状态,具体代码如下:
main.xml:  
<Button  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:text="TestShapeButton"  
    android:background="@drawable/button_selector"  
    />  



button_selector.xml:
<?xml version="1.0" encoding="utf-8"?>  
<selector  
    xmlns:android="http://schemas.android.com/apk/res/android">  
    <item android:state_pressed="true" >  <!-- 按下的时候 -->
        <shape>  
            <!-- 渐变 -->  
            <gradient  
                android:startColor="#ff8c00"  
                android:endColor="#FFFFFF"  
                android:type="radial"  
                android:gradientRadius="50" />  
            <!-- 描边 -->  
            <stroke  
                android:width="2dp"  
                android:color="#dcdcdc"  
                android:dashWidth="5dp"  
                android:dashGap="3dp" />  
            <!-- 圆角 -->  
            <corners  
                android:radius="20dp" />  <!-- 值越大越园 -->
            <padding  
                android:left="10dp"  
                android:top="10dp"  
                android:right="10dp"  
                android:bottom="10dp" />  
        </shape>  
    </item>  
    <item android:state_focused="true" >  <!-- 获取焦点的时候 -->
        <shape>  
            <gradient  
                android:startColor="#ffc2b7"  
                android:endColor="#ffc2b7"  
                android:angle="270" />  
            <stroke  
                android:width="2dp"  
                android:color="#dcdcdc" />  
            <corners  
                android:radius="2dp" />  
            <padding  
                android:left="10dp"  
                android:top="10dp"  
                android:right="10dp"  
                android:bottom="10dp" />  
        </shape>  
    </item>  
    <item>        <!-- 正常的时候 -->       
        <shape>  
            <solid android:color="#ff9d77"/>  
            <stroke  
                android:width="2dp"  
                android:color="#fad3cf" />  
            <corners  
                android:topRightRadius="5dp"  
                android:bottomLeftRadius="5dp"  
                android:topLeftRadius="0dp"  
                android:bottomRightRadius="0dp"  
            />  
            <padding  
                android:left="10dp"  
                android:top="10dp"  
                android:right="10dp"  
                android:bottom="10dp" />  
        </shape>  
    </item>  
</selector>  

ubuntu18.04安装xenomai3.1(百分百成功版) ubuntu18.04安装xenomai3.1(百分百成功版),保姆级教学!!! 阅读详情

相关推荐

国密https握手协议抓包及流程详解

使用的密码套件清单-Cipher Suite,这里服务器端选择了ECC_SM4_CBC_SM3密码套件,也就是SM2公钥算法、SM4-CBC分组密码算法和SM3杂凑算法。具体来说,这里SM2算法密钥交换算法,SM4是加密算法(SM4-CBC)、SM3是校验算法(国标要求为HMAC-SM3)。服务器的加密证书:加密证书中在同样在扩展字段中KeyUsage标识出KeyEncipherment为true和dataEncipherment为true,即数字证书中包含的公钥可用来做密钥加密密钥和数据密钥。

ryanzzzzz的博客 5847

Android中shape的使用(自定义艺术效果,简单好用)

shape形状之意,可自定义各种形状,如背景椭圆,圆角等等 创建目录:drawable–右键–new–drawable resourse file–键入文件名my_shape–ok–修改selector标签为shape 1圆角 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners

xiyangyang8110的博客 1945

[南大ICS-PA2] 字符串处理函数和printf实现

[南大ICS-PA2] 程序、运行时环境与AM 字符串处理函数和printf实现

qq_42474104的博客 2987

Android UI美化之 shape的使用及其属性总结

Android UI美化之 shape的使用及其属性总结 近期在整理项目的过程中突然发现项目中有好多未曾使用过的图片,经过对比UI设计图片并检查代码后发现在UI上的这些地方都是通过drawable 下面的资源文件设置的背景,于是便想起来整理一下关于此类UI美化的东西,若存在写的不好的地方还希望大家多多指导。 本系列预计共3部分,本文具体内容为 shape 的使用,由于另外两篇文章暂时未整理,此...

陈增庆的博客 577

Seedance2.5本地部署:从环境配置到批量生图的工程实践指南

最近在几个技术社群里,看到不少人在讨论 AI 生图和视频工具。有人抱怨在线服务要么贵、要么慢、要么生成内容受限;也有人尝试了各种“免费”方案,结果不是功能残缺就是暗藏收费陷阱。就在大家觉得“又好又免费”的本地部署方案门槛太高时,Seedance2.5 突然冒了出来,而且口号相当直接:本地部署、完全免费、不联网、功能对标甚至超越一些付费产品。 但这类宣传我们见多了,真正的问题是:它到底能不能在普通开发者的机器上跑起来?生成质量是否真的可用?长期使用会不会遇到模型崩溃、依赖冲突或者资源爆炸?更重要的是,对于一个

weixin_27419879的博客 1129

MATLAB控制信号发生器(USB-GPIB-HS)GPIB仪器控制教程(含matlab代码)

自动化测试时需要自动产生信号,通过matlab控制信号发生器的频率与功率,以实现自动化信号生成输出

qq_43686057的博客 1606

Android使用shape绘制各种形状

在开发中经常会用到shape标签来定义控件的背景,好处是减少apk的占内存大小,shape标签总共有四个图形选项,分别是rectangle(矩形),oval(椭圆),line(横线)和ring(圆环)。 res下新建一个Drawable resource file: 矩形效果: &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;sha...

weixin_39654467的博客 7515

Chronos时间序列预测实战:从零开始微调你的第一个大语言模型(附完整代码)

本文提供了一份详细的Chronos时间序列预测大语言模型微调实战指南。从环境搭建、数据预处理到模型配置与训练,手把手教你如何利用自己的数据微调Chronos模型,并完成评估与部署,助你快速掌握这一前沿技术。

zzz56的博客 300

Android控件样式美化之shape的使用

Android提供的控件外观实在不怎么好看,比如一个Button,一个EditText。那么我们可以自定义这些控件的背景来达到美观的效果。 常用的方法就是利用shape,shape听起来是用来改变形状的,它不但可以改变形状,还可以改变好多东西。 下面就开始写一个shape文件吧 我们右击drawable选择new一个Drawable resource file,我们会看到它第二行默认为sele

Key_MQL的博客 1405

硬件工程师八大细分岗位详解:从芯片到整机的技术分工与职业选择

硬件工程作为电子系统设计的核心领域,涉及从芯片级到整机级的完整技术链条。其基本原理是通过电路设计、信号处理和功率管理等技术,将电子元件集成为功能完备的硬件系统。随着产品复杂度提升,硬件工程师岗位出现高度专业化分工,这种细分不仅提升了技术深度,更优化了团队协作效率。在工程实践中,信号完整性和电源完整性成为高速数字设计的两个关键技术挑战,直接影响系统稳定性。本文基于行业热词"硬件工程师"和"信号完整性",深入解析八大细分岗位的技术差异,帮助从业者明确职业发展方向,构建系统化的硬件设计思维。

weixin_33811539的博客 450

35、使用 Geo 创建 GitLab 的分布式只读副本

本文详细介绍了如何使用GitLab Geo功能创建分布式只读副本,涵盖数据库主从配置、SSH密钥查找方式更改、通过Web UI添加从节点、激活哈希存储及状态检查等关键步骤。同时提供了常见问题解答和配置流程图,帮助用户快速部署并维护Geo集群,实现跨地域的高效数据同步与高可用架构。

chair的专栏 81

Android自定义图形-Shape

1 概述 1.1 编写目的 学习Android自定义图形shape相关的知识,记录下来,方便后续学习及查询。 2 Shape图形 Android上Shape有以下几种属性shape、 corners、 gradient、 padding、size、solid、stroke,其中shape为一级属性,其余为shape子属性,子属性可以选择定义。基本结构如下图:

zqilli的博客 6088

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

Android美化插件,Android控件美化Shape

Android控件美化Shape你会用吗? -- 未审核编辑文档如果你对Android系统自带的UI控件感觉不够满意,可以尝试下自定义控件,我们就以Button为例,很早以前Android123就写到过Android Button按钮控件美化方法里面提到了xml的selector构造。当然除了使用drawable这样的图片外今天Android开发网谈下自定义图形shape的方法,对于Butto...

weixin_30125497的博客 392

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

Android UI美化之Shape Drawable的使用

想美化一下程序里的EditText,发现网上关于美化的文章较少,即使有也大多是直接翻译文档,索性一边研读一边写例子。 先看一下文档对Shape Drawable的描述: Shape DrawableAn XML file that defines a geometric shape, including colors and gradients. Creates aShapeDrawabl

司光鹏的技术博客 1184

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

Android中在xml中使用shape美化UI

shape:定义控件的几何形状,如定义一个圆角的Button,设置渐变色等;当使用shape时,在res/drawable下新建.xml文件选择shape; 一.shape中的属性: <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval":椭圆形状 sh

FightFightFight的博客 1163

android 代码 shape,Android Shape控件美化实现代码

如果你对Android系统自带的UI控件感觉不够满意,可以尝试下自定义控件,我们就以Button为例,很早以前Android123就写到过Android Button按钮控件美化方法里面提到了xml的selector构造。当然除了使用drawable这样的图片外今天Android开发网谈下自定义图形shape的方法,对于Button控件Android上支持以下几种属性shape、gradient、...

weixin_29468281的博客 188

Android控件美化Shape

如果你对Android系统自带的UI控件感觉不够满意,可以尝试下自定义控件,我们就以Button为例,很早以前Android123就写到过Android Button按钮控件美化方法里面提到了xml的selector构造。当然除了使用drawable这样的图片外今天Android开发网谈下自定义图形shape的方法,对于Button控件Android上支持以下几种属性sha...

weixin_33744141的博客 92

Android控件美化Shape你会用吗?

<br /><br /> 如果你对Android系统自带的UI控件感觉不够满意,可以尝试下自定义控件,我们就以Button为例,很早以前Android123就写到过Android Button按钮控件美化方法里面提到了xml的selector构造。当然除了使用drawable这样的图片外今天Android开发网谈下自定义图形shape的方法,对于Button控件Android上支持以下几种属性shape、gradient、stroke、corners等。<br />  我们就以目前系统的Button的sel

tiantianshangcha的专栏 655

Android的shape美化

Eg1:shape使android组件呈现特殊效果       使用到的布局文件  view plaincopyprint?      android:layout_width="fill_parent"      android:layout_height="fill_parent"      android:orientation="vertical"

lisalty的专栏 533

android界面的美化shape的使用

Shape Drawable 这一类的shape定义在xml中 file location: res/drawable/filename.xml The filename is used as the resource ID.(这个文件名作为资源id) compiled resource datatype:(复杂资源的数据类型) Resource pointer to a 

键盘的起始页 1191
上一篇: Android中的Animation应用
下一篇: android中文字跑马灯效果
imdxt1986
博客等级 码龄16年 64粉丝 77原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值