Android 2.2 - how do i set an spinner's alpha

2019-05-21 08:19发布

问题:

I'm trying to make a spinner totally transparent. In Android 4.0 I can do this setting the alpha property to 0 in the xml layout designer. But when I work with Android 2.2 i can't use that property, Eclipse mark it as an error and tell me that i can't use it.

I tried to make it transparent writting this java code:

final Spinner cmbCategorias = (Spinner) findViewById(R.id.cmbCategorias);
cmbCategorias.getBackground().setAlpha(0);

and it works, but the text in the spinner keeps visible.

Someone can tell me what can i do?? Thanks

回答1:

Make xml Layout like spinner_textview.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/txtview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:singleLine="true"
    android:textColor="@android:color/transparent" />

And add the following in Java Code:

Spinner sp=(Spinner)findViewById(R.id.sp);   
  sp.setAdapter(new ArrayAdapter(this,R.layout.spinner_textview, 
                items));


回答2:

I did a function like this:

private void enableView(View v, boolean enable)
{
    if(v.isEnabled() == enable)
        return;

    float from = enable ? .5f : 1.0f;
    float to = enable ? 1.0f : .5f;

    AlphaAnimation a = new AlphaAnimation(from, to);

    a.setDuration(500);
    a.setFillAfter(true);

    v.setEnabled(enable);
    v.startAnimation(a);
}

It works for spinners too.