【问题标题】:Spinner popUpBackground cannot setting to transparent color in AndroidSpinner popUpBackground 无法在 Android 中设置为透明色
【发布时间】:2014-12-04 20:40:52
【问题描述】:

第一次使用自定义微调器。我有一个使用九个补丁图像的背景微调器。 Spinnermode 处于对话模式。现在我为微调器和微调器项目设置自定义布局。运行代码,但我没有想要的解决方案。即使我设置了微调项的背景,微调项的背景上也有角落。搜索了很多,但没有找到确切的解决方案。我使用了来自该站点 sourcecode form this site 的代码,它们的结果是完美的,但是当我在那里尝试时,整个代码没有得到想要的结果,在微调项中看到白色背景。

ma​​in.xml

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/scrollView"
        android:layout_marginLeft="20dp"
        android:background="@drawable/dropdown_button"
        android:paddingLeft="2dp"
        android:paddingRight="13dp"
        android:paddingTop="5dp"
       android:spinnerMode="dialog" />
</RelativeLayout>

spinner_item.xml

<?xml version="1.0" encoding="UTF-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/spinneritem"
    android:layout_width="100dp"
    android:layout_height="fill_parent"
    android:paddingLeft="10dp" />

spinner_item_list

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/blank_buttons"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/spinneritemlist"
        style="@style/SpinnerText"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center_vertical|center_horizontal"
        android:gravity="center"
        android:textColor="@android:color/black" />

</LinearLayout>

**in onCreate()**



ArrayList<CountryInfo> myCountries;
        myCountries = populateList();spinner1 = (Spinner) findViewById(R.id.spinner1);
        CountryAdapter myAdapter = new CountryAdapter(this, R.layout.spinner_item, myCountries);
        spinner1.setAdapter(myAdapter);

函数和适配器

 public ArrayList<CountryInfo> populateList()
        {
            ArrayList<CountryInfo> myCountries = new ArrayList<CountryInfo>();
            myCountries.add(new CountryInfo("USA")); // Image stored in /drawable
            myCountries.add(new CountryInfo("Sweden"));
            myCountries.add(new CountryInfo("Canada"));
            return myCountries;
        }

    public class CountryAdapter extends ArrayAdapter<CountryInfo>
    {
        private Activity context;
        ArrayList<CountryInfo> data = null;

        public CountryAdapter(Activity context, int resource, ArrayList<CountryInfo> data)
        {
            super(context, resource, data);
            this.context = context;
            this.data = data;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) 
        {   // Ordinary view in Spinner, we use android.R.layout.simple_spinner_item
            return super.getView(position, convertView, parent);   
        }

        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent)
        {   // This view starts when we click the spinner.
            View row = convertView;
            if(row == null)
            {
                LayoutInflater inflater = context.getLayoutInflater();
                row = inflater.inflate(R.layout.spinner_item_list, parent, false);
            }

            CountryInfo item = data.get(position);

            if(item != null)
            {   // Parse the data from each object and set it.

                TextView myCountry = (TextView) row.findViewById(R.id.spinneritemlist);

                if(myCountry != null)
                    myCountry.setText(item.getCountryName());

            }

            return row;
        }
    }

我生成的图像:

他们网站上示例代码的完美结果的打印屏幕(不是运行他们的代码)

示例代码不完美结果的屏幕截图(在我的应用中运行他们的代码)

【问题讨论】:

    标签: android spinner android-spinner


    【解决方案1】:

    试试这个

    <Spinner
            android:id="@+id/spinner1"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:layout_below="@+id/scrollView"
            android:layout_marginLeft="20dp"
            android:background="@drawable/dropdown_button"
            android:paddingLeft="2dp"
            android:paddingRight="13dp"
            android:paddingTop="5dp"
            android:spinnerMode="dialog"
            android:popupBackground="@android:color/transparent" />//or desired color or drawable
    

    也可以在 spinner_list_item 中试试这个

        <?xml version="1.0" encoding="UTF-8"?>
       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:orientation="horizontal" >
    
        <TextView
            android:id="@+id/spinneritemlist"
            style="@style/SpinnerText"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center_vertical|center_horizontal"
            android:gravity="center"
            android:background="@drawable/blank_buttons"
            android:textColor="@android:color/black" />
    
    </LinearLayout>
    

    【讨论】:

    • 我之前也做过,但是效果还是一样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-02
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多