首次使用自定义微调器。我有一个背景使用九个补丁图像的微调器。 Spinnermode处于对话模式。现在我为微调器和微调器项设置自定义布局。运行代码,但我没有想要的解决方案。即使我已经设置了微调器项目的背景,也可以在微调器项目的背景上看到角落。搜索了很多,但没有找到确切的解决方案。我使用了这个网站sourcecode form this site的代码,他们的结果非常完美,但是当我在那里尝试时,整个代码都没有在微调器项目中看到白色背景所需的结果。
main.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;
}
}
我的结果图片:
在其网站上打印出示例代码完美结果的屏幕(而不是运行代码)
示例代码非完美结果的截图(来自在我的应用中运行代码)
答案 0 :(得分:0)
试试这个
<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>