Button opacity/transparency

点点圈 提交于 2019-11-30 10:20:51
CSmith

check How to Set Opacity (Alpha) for View in Android

You can set transparency on background of objects, use #XX808080, where XX is the alpha/transparency value.

android:background="#80FF0000"

this will be a half-transparent red background.

You can set transparency on the button text using the textColor property in the same manner:

android:textColor="#80FF0000"

You can also achieve through code in an animation

AlphaAnimation alphaAnim = new AlphaAnimation(1.0f, 0.5f);
alphaAnim.setDuration (400);
myButton.getBackground().startAnimation(alphaAnim);  // set alpha on background

or directly:

myButton.getBackground().setAlpha(0.5f);   // added in API 11, sets alpha on background

both methods set the alpha to 50%

Lastly, you can try adding android:alpha for your XML:

<Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/zece"
        android:textStyle="bold"
        android:textColor="#808080" 
android:alpha="0.5"
/>

You probably cant change its opacity with code, because they are 9patches. You need to create your own 9patch and set it to buttons as background. If you want a solid color, of course you can also use something like:

android:background="#80363636"

Here #AARRGGBB, how low you keep first two digits you get that opacity

in code, you can get an instance of the button and set the background drawable's alpha manually.

 Button btn = (Button) findViewById(..);
 btn.getBackground().setAlpha( a );  // where a : 0..255 : 0=transparent, 255=fully opaque
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!