gpt4 book ai didi

android - 如何更改 android 中下拉 View 的标题?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:12:45 25 4
gpt4 key购买 nike

我的布局中有一个微调器并使用 android:prompt="@string/year" 设置提示。

默认提示如下:

Default prompt looks as

现在我的问题是:

  1. 如何将默认图标更改为其他图片?
  2. 如何让“年”文字居中显示为红色?
  3. 如何将背景颜色更改为黄色(存在“年”文字和图标的背景)?

我的自定义适配器类

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com