作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我的布局中有一个微调器并使用 android:prompt="@string/year"
设置提示。
默认提示如下:
现在我的问题是:
我的自定义适配器类
private class MyCustomSpinnerAdapter extends ArrayAdapter<ArrayList> {
private ArrayList<String> objectsList = new ArrayList<String>();
@SuppressWarnings("unchecked")
public MyCustomSpinnerAdapter(Context context, int textViewResourceId,
ArrayList objects) {
super(context, textViewResourceId, objects);
this.objectsList = objects;
}
@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 getCustomView1(position, convertView, parent);
}
public View getCustomView(int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View rowView = inflater.inflate(R.layout.spinner_dropdown, parent,
false);
LinearLayout rowTitle = (LinearLayout) rowView
.findViewById(R.id.row);
rowTitle.setBackgroundResource(R.drawable.spinner_row_focused);
TextView textView = (TextView) rowView.findViewById(R.id.title);
textView.setTypeface(typeFace);
textView.setText(objectsList.get(position).toString().trim());
return rowView;
}
public View getCustomView1(int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View rowView = inflater.inflate(R.layout.spinner_dropdown, parent,
false);
TextView textView = (TextView) rowView.findViewById(R.id.title);
textView.setText(objectsList.get(position).toString().trim());
textView.setTypeface(typeFace);
return rowView;
}
}
最佳答案
这是您的解决方案:为页眉创建简单布局(文件 R.layout.spinner_head
):
<ImageView
android:id="@+id/spin_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:padding="5dip"
android:src="@android:drawable/btn_star"
android:layout_alignParentLeft="true"/>
<TextView
android:id="@+id/title"
android:textColor="#f00"
android:gravity="center"
android:text="Year"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
创建自定义微调器:
class MySpinner extends Spinner {
public MySpinner(Context context) {
super(context);
}
@Override
public boolean performClick() {
boolean handled = false;//super.performClick();
if (!handled) {
handled = true;
Context context = getContext();
AlertDialog.Builder builder = new AlertDialog.Builder(context);
CharSequence mPrompt = getPrompt();
if (mPrompt != null) {
builder.setCustomTitle(getLayoutInflater().inflate(R.layout.spinner_head, null));
}
builder.setSingleChoiceItems(new DropDownAdapter(getAdapter()),getSelectedItemPosition(), this).show();
}
return handled;
}
@Override
public void onClick(DialogInterface dialog, int which) {
setSelection(which);
dialog.dismiss();
}
class DropDownAdapter implements ListAdapter, SpinnerAdapter {
private SpinnerAdapter mAdapter;
public DropDownAdapter(SpinnerAdapter adapter) {
this.mAdapter = adapter;
}
@Override
public int getCount() {
return mAdapter == null ? 0 : mAdapter.getCount();
}
@Override
public Object getItem(int position) {
return mAdapter == null ? null : mAdapter.getItem(position);
}
@Override
public long getItemId(int position) {
return mAdapter == null ? -1 : mAdapter.getItemId(position);
}
@Override
public int getItemViewType(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getDropDownView(position, convertView, parent);
}
@Override
public int getViewTypeCount() {
return 1;
}
@Override
public boolean hasStableIds() {
return mAdapter != null && mAdapter.hasStableIds();
}
@Override
public boolean isEmpty() {
return getCount() == 0;
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
if (mAdapter != null) {
mAdapter.registerDataSetObserver(observer);
}
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
if (mAdapter != null) {
mAdapter.unregisterDataSetObserver(observer);
}
}
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
return mAdapter == null ? null : mAdapter.getDropDownView(position, convertView, parent);
}
@Override
public boolean areAllItemsEnabled() {
return true;
}
@Override
public boolean isEnabled(int arg0) {
return true;
}
}
}
创建微调器的实例
MySpinner theSpin = new MySpinner(this);
theSpin.setPrompt("Select Item");
ArrayAdapter<String> theAdapter =
new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, new String[]{"item1","item2","item3","item4","item5","item6" });
theSpin.setAdapter(theAdapter);
现在使用这个将显示自定义标题的自定义微调器。您可以根据需要修改该 header 。
关于android - 如何更改 android 中下拉 View 的标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12778102/
前文链接: Spring复杂的BeanFactory继承体系该如何理解? ----上 Spring复杂的BeanFactory继承体系该如何理解? ----中 Spring IoC容器 Applica
我正在努力实现这个目标 | Div | |Div nav wrapper| | logo | |con
我正在尝试从下拉菜单中的链接进行模式启动。模态似乎启动了(网站变灰),但看不到。来自不在下拉列表中的常规链接的模式工作得很好。我愚弄了 jquery,但由于我是 jquery 的新手,所以没有任何结果
我是一名优秀的程序员,十分优秀!