【问题标题】:How to change the background transparency of a standard system button如何更改标准系统按钮的背景透明度
【发布时间】:2012-11-17 15:05:50
【问题描述】:

我的一个活动中有一组按钮。这些按钮看起来不错,因为它们来自 Android 系统(灰色),因为我的活动的背景是灰色的。我已经尝试了以下

button.setBackgroundColor(Color.argb(125, 200, 200, 200));

这会创建所需的透明度,但我会丢失按钮状态,例如按下、向上等。

如何保持 android 附带的按钮状态并且只降低不透明度?

【问题讨论】:

    标签: android button view android-widget


    【解决方案1】:

    在您的班级中,使用以下代码:

    alpha = new AlphaAnimation(0.3F, 0.3F); //Set opacity - Range 0.0 to 1.0
    alpha.setDuration(0); // Set animation duration
    alpha.setFillAfter(true); // Maintaining the effect to the button
    yourButton.startAnimation(alpha);
    

    希望我能帮上忙。

    再见!

    【讨论】:

    • @Tanyna Bonaldo 是的,你帮了忙,很棒的伙伴,正是我想要的,谢谢;)
    【解决方案2】:

    在所需按钮的 xml 中,您应该可以简单地放置:

    android:background="@android:color/transparent"

    我用它来显示没有默认灰色背景的drawable。

    -干杯

    【讨论】:

    • 警告这将丢弃所有背景...甚至不会有轮廓,因为整个按钮图像实际上是背景
    【解决方案3】:

    动态方式:

    Button.getBackground(),如果将位图设置为背景,通常返回 BitmapDrawable,如果将颜色设置为背景,则返回 ColorDrawable。

    ColorDrawable colorDrawable = (ColorDrawable) button.getBackground();
    colorDrawable.setAlpha(125);

    这样就可以了。

    静态方式:
    在布局中,设置背景颜色如#7EC8C8C8

    【讨论】:

    • 嗨,它确实有效,但它是使用动画更改不透明度的最佳方式吗?我认为如果你改变背景drawable的alpha通道会更好。
    猜你喜欢
    • 1970-01-01
    • 2015-02-22
    • 1970-01-01
    • 1970-01-01
    • 2018-04-07
    • 2017-06-24
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    相关资源
    最近更新 更多