设置button背景的半透明效果!!!

这篇博客介绍了如何在Android中为Button设置半透明背景。通过修改XML布局文件,设置Button的背景为一个资源图,并在Java代码中使用阿尔法滤镜setAlpha()方法调整透明度,达到半透明效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

大家好,今天我们来说下怎样做button的半透明背景效果!很简单的!

首先,给大家贴上main.xml的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/bg"><!--android:background="@drawable/bg这里为了增强对比度才用的我资源中的一个图片-->

 <Button
     android:background="@drawable/ic_launcher"
     android:layout_width="300dip"
        android:layout_height="200dip"
        android:textColor="#fff"
        android:text="button半透明背景"
        android:id="@+id/button1"
        />

</LinearLayout>

 然后,给大家贴上java代码:

package com.guo.myGridView;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;

public class MyGridViewTestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        Button b = (Button)findViewById(R.id.button1);;

        b.getBackground().setAlpha(35);//这里利用阿尔法滤镜设置button背景的透明度
    }
}

ok了!运行吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值