我尝试创建一个Spinner来选择颜色。颜色应显示在Spinner标题中。这是一个截图:
但正如您所看到的,颜色不会填充我的9补丁Spinner背景(标题)。这是9补丁背景:
但我不明白为什么......
我实现了一个自定义的ArrayAdapter,这里是代码:
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new MyArrayAdapter(this, R.layout.spinner_row, R.id.textView1, items);
spinner.setAdapter(adapter);
private class MyArrayAdapter extends ArrayAdapter<String>{
public MyArrayAdapter(Context context, int resource, int textViewResourceId, String[] objects) {
super(context, resource, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomViewSmall(position, convertView, parent);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.spinner_row, parent, false);
View v = (View) row.findViewById(R.id.spinnerFrame);
v.setBackgroundColor(getColor(position));
return row;
}
public View getCustomViewSmall(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.spinner_row_small, parent, false);
row.setBackgroundColor(getColor(position));
return row;
}
private int getColor(int pos) {
switch(pos) {
case 0:
return colors[0]; // Blue
case 1:
return colors[1]; // Purple
case 2:
return colors[2]; // Orange
case 3:
return colors[3]; // Yellow
case 4:
return colors[4]; // Cyan
}
return colors[5];
}
}
以下是布局代码:
spinner_row_small.xml(即标题的布局):
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinnerFrameSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
spinner_row.xml(行的布局):
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/FrameLayout1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="center"
android:background="@color/white" >
<FrameLayout
android:id="@+id/spinnerFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</FrameLayout>
</FrameLayout>
和main.xml:
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/textview"
android:spinnerMode="dropdown"
android:popupBackground="@drawable/spinner_background"
android:layout_marginBottom="30dp" />
如果有人能帮助我,我会很高兴 最好的问候!
答案 0 :(得分:1)
我的代码中没有看到任何错误,但nine patch image
的问题是,只有标记的空间会被放大,您只能获得实际使用的标记空间。这就是为什么你要在所有方面获得空间(没有标记)。我认为九个补丁图像对于简单的背景很有用,没有太多的颜色复杂性。