我正在动态创建一个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>
coordinationspinnerlayout.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:添加更多代码。
答案 0 :(得分:0)
编辑:
您正在使用微调器,因此您需要实现[getDropDownView][1]
。
如果要更改视图的选定颜色,则需要创建一个颜色列表Drawable,如此
在Colors / list_color.xml
中<selector>
<item android:state_selected="true">SomeColor</item>
</selector>
在您的视图中
<YourView
android:background="@color/list_color" />
有关详细信息,请参阅this resource
答案 1 :(得分:0)
经过长时间寻找解决方案,
我发现我无法设置样式或动态更改其选择颜色因为我正在开发的API版本。
答案 2 :(得分:0)
take the custum adapter
mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int position, long arg3) {
_position = position;
LinearLayout linear_ayout = (LinearLayout)parent.getChildAt(0);
linear_ayout.setBackgroundColor(Color.TRANSPARENT);
TextView tv = (TextView) linear_ayout.getChildAt(0);
tv.setTextColor(Color.WHITE);
gname_spinner=mySpinner.getSelectedItem().toString();
if (mySpinner.getSelectedItem().toString().equalsIgnoreCase("All Groups"))
filItemAdapter("A", "");
else
filItemAdapter("G", mySpinner.getSelectedItem().toString());
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
Logger.d("******", "not selected");
filItemAdapter("A", "");
}
});
MyCustomAdapter custom_adapter = new MyCustomAdapter(
getApplicationContext(), R.layout.custum_spinner_group,
group_list);
mySpinner.setAdapter(custom_adapter);
public class MyCustomAdapter extends ArrayAdapter<String> {
List<String> group_list;
public MyCustomAdapter(Context context, int textViewResourceId,
List<String> group_list) {
super(context, textViewResourceId, group_list);
this.group_list = group_list;
}
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(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.custum_spinner_group, parent, false);
TextView text_group = (TextView) row.findViewById(R.id.text_group_name);
LinearLayout layout = (LinearLayout) row
.findViewById(R.id.layout_group_name);
text_group.setText(group_list.get(position));
if (position == _position) {
layout.setBackgroundColor(getResources().getColor(R.color.spinner_background));
text_group.setTextColor(getResources().getColor(R.color.white));
} else {
layout.setBackgroundColor(Color.TRANSPARENT);
text_group.setTextColor(getResources().getColor(R.color.spinner_background));
}
Log.d("selected group", ":"+text_group.getText());
return row;
}
}