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

2019-05-21 08:00发布

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

2条回答
女痞
2楼-- · 2019-05-21 08:40

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.

查看更多
Evening l夕情丶
3楼-- · 2019-05-21 08:41

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));
查看更多
登录 后发表回答