Android 透明Button

1、是制作9-patch的图片,这样能够匹配文字内容的长短。

2、是指定按钮样式背景,即定制drawable的xml文件,这样做的好处不用图片做背景,节省空间。

定制透明样式的按钮。直接看代码:

drawable/buttonstyle.xml:

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="true" android:state_pressed="true">
        <shape android:shape="rectangle">
            <corners android:radius="8dp"/>
            <solid android:color="#d35400"></solid>
            <size android:height="32dp" android:width="72dp"></size>
            <padding android:bottom="5dp" android:left="5dp"
     android:right="5dp" android:top="5dp">
            </padding>
        </shape>
    </item>

    <item android:state_enabled="true" android:state_pressed="false">
        <shape android:shape="rectangle">
            <corners android:radius="8dp"/>
            <size android:height="32dp" android:width="72dp"></size>
            <padding android:bottom="5dp" android:left="5dp"
             android:right="5dp" android:top="5dp"></padding>
            <solid android:color="#88f39c12"></solid>
        </shape>
    </item>
</selector>

看到有2个item结构,第一个是按钮按下状态样式,第二个是普通状态下按钮的状态,item结构中包含shape标签,android常用shape来改变控件的一些样式。

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为角的弧度,值越大角越圆。

    size:设置大小

    padding:设置内部间距

在进行solid中设置颜色的时候,颜色的格式为#+16进制透明度+16进制颜色数值,如:33FFFFFF,33为alpha值,用来设置透明度,数值越小,越透明。后六位F为用16进制表示的颜色值。所以设置透明度的简单方式就是在设置颜色的时候设置它的前2位。

最后效果如下图:

时间: 2024-08-04 08:37:29

Android 透明Button的相关文章

android 设置Button或者ImageButton的背景透明 半透明 透明

Button或者ImageButton的背景设为透明或者半透明 半透明<Button android:background="#e0000000" ... /> 透明<Button android:background="#00000000" ... /> 颜色和不透明度 (alpha) 值以十六进制表示法表示.任何一种颜色的值范围都是 0 到 255(00 到 ff).对于 alpha,00 表示完全透明,ff 表示完全不透明.表达式顺序是

[Android]使用button切换TextView可见和不可见

<Button android:id="@+id/btnTest" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button" /> <TextView android:id="@+id/myTestText" android:layout_width=&quo

Android 透明的状态栏有渐变的阴影效果

前言 欢迎大家我分享和推荐好用的代码段~~ 声明          欢迎转载,但请保留文章原始出处: CSDN:http://www.csdn.net 雨季o莫忧离:http://blog.csdn.net/luckkof 正文 状态栏透明后有渐变的阴影效果,如何去除? 这是google default设计,如果桌面壁纸是白色的时候比较明显,这个渐进的效果是通过背景图来设置的,在frameworks/base/packages/systemui/res/ 里面drawable-hdpi(如果是其

Android中Button的五种监听事件

简单聊一下Android中Button的五种监听事件: 1.在布局文件中为button添加onClick属性,Activity实现其方法2.匿名内部类作为事件监听器类3.内部类作为监听器4.Activity本身作为事件监听器,实现onClickListener5.外部类作为监听器 ButtonListenerActivity.class public class ButtonListenerActivity extends AppCompatActivity implements View.On

Android 的 Button 按钮实现的两种方式

第一组方式: res/laoyout/activity_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=&

android: button 属性

代码中常用属性: Button bt=new Buttion(Context context); setClickable(boolean clickable) 设置按钮是否允许点击. clickable=true:允许点击 clickable=false:禁止点击 setBackgroundResource(int resid) 通过资源文件设置背景色. resid:资源xml文件ID. 按钮默认背景为:android.R.drawable.btn_default setText(CharSe

转:在android中button响应的两种方式

1. 在布局文件中添加button的监听名字 Android:onClick="buttonOnClick" 例如: <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/button1&q

java.lang.ClassCastException:android.widget.Button cannot be cast to android.widget.ImageView

今天遇到一个错误也不知道怎么回事,上网搜了一下: 出现的问题是:java.lang.ClassCastException:android.widget.Button cannot be cast to android.widget.ImageView 解决的方法是: Clean 你的项目.(点击eclipse上面的project然后clean然后选择你要clean的项目) 修改一个xml文件,然后保存(稍微动一下,保存) 删除 R 文件. (build project 之后会自动生成).

Android添加button并响应

1.首先要在activity_main.xml文件里新建两个button标签. 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_hei