我正在动态创建一个 Spinner,如下面的代码:
private void createCoordinationSpinner() {
TextView tvwCoordination = new TextView(this);
this.spinnerCoordination = new Spinner(this);
//Some code to setup textview...
LinearLayout.LayoutParams paramsSpinnerCoordination = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
paramsSpinnerCoordination.setMargins(10, 0, 10, 0);
this.spinnerCoordination.setLayoutParams(paramsSpinnerCoordination);
this.spinnerCoordination.setBackgroundResource(R.drawable.rounded_border);
this.spinnerCoordination.setPrompt("Coordination");
this.spinnerCoordination.setOnItemSelectedListener(spinnerItemClickListener);
//Adding both views to an existing LinearLayout, that is possible to have another views, instead of spinner
linearSchoolCoordination.addView(tvwCoordination, 0);
linearSchoolCoordination.addView(spinnerCoordination, 1);
linearSchoolCoordination.setGravity(Gravity.BOTTOM);
}
//Thats the line that sets the adapter:
adapterCoordination = new ItemSpinnerAdapter(context, R.layout.coordinationspinnerlayout, arrayCoordination);
rounded_border.xml:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/gray_dialog" />
<padding android:left="8dp"
android:top="8dp"
android:right="8dp"
android:bottom="8dp" />
<corners android:radius="20dp" />
</shape>
协调微调器布局.xml:
<LinearLayout android:orientation="vertical"
android:background="@color/transparent2"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/transparent2"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/transparent2"
android:gravity="center_vertical" >
<ImageView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/imageSpinnerItem"
android:src="@drawable/itemlistviewicon_nochildren"
android:layout_marginTop="5dp"
android:layout_marginLeft="8dp"
android:layout_marginBottom="5dp"/>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/tvwSpinnerCoordination"
android:layout_marginLeft="10dp"
android:textSize="16dip"
android:textColor="@color/black"
android:text="TextView"
android:layout_weight="1"/>
<ImageView android:layout_height="20dp"
android:layout_width="20dp"
android:id="@+id/imageSpinnerDownArrow"
android:src="@drawable/spinnerdownarrow"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginRight="10dp"/>
</LinearLayout>
<View android:background="@color/silver"
android:layout_height="1dp"
android:layout_width="fill_parent"
android:id="@+id/linearSpinnerSeparator"/>
</LinearLayout>
</LinearLayout>
最后是适配器 ItemSpinnerAdapter.java:
public class ItemSpinnerAdapter extends ArrayAdapter<CoordinationData>
{
private int tvwID;
private List<CoordinationData> coordinationList;
public ItemSpinnerAdapter(Context context, int textViewResourceId, List<CoordinationData> coordinationList)
{
super(context, textViewResourceId, coordinationList);
this.tvwID = textViewResourceId;
this.coordinationList = coordinationList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
View view = convertView;
if (view == null)
{
LayoutInflater layInf = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layInf.inflate(tvwID, null);
holder = new ViewHolder();
holder.tvwDescription = (TextView) view.findViewById(R.id.tvwSpinnerCoordination);
holder.description = coordinationList.get(position).getCoordinationName();
view.findViewById(R.id.linearSpinnerSeparator).setVisibility(View.GONE);
view.setTag(holder);
}
else
{
holder = (ViewHolder) view.getTag();
holder.description = coordinationList.get(position).getCoordinationName();
}
if (holder.tvwDescription != null)
holder.tvwDescription.setText(holder.description);
return view;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
View view = convertView;
if (view == null)
{
LayoutInflater layInf = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layInf.inflate(tvwID, null);
holder = new ViewHolder();
holder.tvwDescription = (TextView) view.findViewById(R.id.tvwSpinnerCoordination);
holder.tvwDescription.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
holder.description = coordinationList.get(position).getCoordinationName();
view.findViewById(R.id.imageSpinnerDownArrow).setVisibility(View.GONE);
view.setTag(holder);
}
else
{
holder = (ViewHolder) view.getTag();
holder.description = coordinationList.get(position).getCoordinationName();
}
if (holder.tvwDescription != null)
holder.tvwDescription.setText(holder.description);
return view;
}
public class ViewHolder
{
TextView tvwDescription;
String description;
}
}
将它添加到我活动的 XML 布局文件中已经存在的 LinearLayout 中。与我的 CustomSpinnerAdapter 一起,它运行良好。但我仍然需要更改一些属性并且我被卡住了......由于我的应用程序使用蓝色文本和背景,我想设置微调项突出显示颜色的选择。它的默认值为橙色。
由于我正在为 API 10 进行开发,因此我使用了有限的 Spinner 方法。
有什么建议么?提前致谢!
编辑:是否可以更改提示标题背景颜色?
EDIT2:添加更多代码。