简体   繁体   English

更改一个按钮的不透明度会更改所有按钮

[英]Changing opacity of one button changes all of them

I've run into this problem in the past, and the way I fixed it last time was just creating a new .png file for every button (even though they are the same). 我过去曾遇到过这个问题,上次修复此问题的方法是为每个按钮创建一个新的.png文件(即使它们相同)。 However, I'm looking for more of a professional, efficient solution. 但是,我正在寻找更多的专业,高效的解决方案。

Basically, I have 7 buttons in my app. 基本上,我的应用程序中有7个按钮。 All of them use background.png as their background. 他们所有人都使用background.png作为背景。 Part of my code tells the app to 'lock' certain buttons until they are unlocked. 我的部分代码告诉应用程序“锁定”某些按钮,直到它们被解锁。 However, when I go to set the alpha of one button, they are all affected. 但是,当我设置一个按钮的Alpha值时,它们都会受到影响。 Is there an easier solution other than creating 6 duplicates of background.png and renaming them? 除了创建6个重复的background.png并重命名它们之外,还有其他更简单的解决方案吗?

My code is pretty standard: 我的代码很标准:

Within my XML layout file: 在我的XML布局文件中:

<Button android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button"
        android:layout_margin="2dp"
        android:layout_weight="1"
        android:text="BUTTON TEXT"
        android:textSize="20dp"
        android:textColor="#FFFFFF"
        android:shadowColor="#000000"
        android:shadowRadius="2"
        android:shadowDx="2"
        android:shadowDy="2"/>

My custom buttom XML file that is referenced for my buttons: 我的按钮引用了我的自定义buttom XML文件:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:state_enabled="false"
    android:drawable="@drawable/button_unpressed" />
<item
    android:state_pressed="true"
    android:state_enabled="true"
    android:drawable="@drawable/button_depressed" />
<item
    android:state_focused="true"
    android:state_enabled="true"
    android:drawable="@drawable/button_unpressed" />
<item
    android:state_enabled="true"
    android:drawable="@drawable/button_unpressed" />
</selector>

Lastly the code I'm using to alter my buttons: 最后,我用来更改按钮的代码:

button.setEnabled(false);
button.setText("BUTTON TEXT");
button.getBackground().setAlpha(122);

Replace 更换

button.getBackground().setAlpha(122);

with

button.setAlpha(122);

Whoops: Inside my custom button XML all I had to do was create a new .png of the button disabled (a bit darker). 糟糕:在我的自定义按钮XML中,我要做的就是创建一个新的.png禁用的按钮(有点暗)。 After this I removed the code to change the alpha value completely, and now all is good! 此后,我删除了代码以完全更改alpha值,现在一切正常! When I disable the button programatically, the color changes to the .png file specified in my XML file. 当我以编程方式禁用该按钮时,颜色更改为XML文件中指定的.png文件。

assign background in codefor every button: 在代码中为每个按钮分配背景:

 Drawable drawable=getResources().getDrawable(R.drawable.button);
    b5.setBackground(drawable);
    drawable=getResources().getDrawable(R.drawable.button);
    b6.setBackground(drawable);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM