Android 单个微调器项的自定义格式不起作用
我希望在Android 单个微调器项的自定义格式不起作用,android,android-spinner,Android,Android Spinner,我希望在微调器中显示的第一个项目上有一个特定的文本外观。 尽管调用并执行了下面的代码,但微调器中的第一项并没有按预期格式化 public class ClubsAdapter extends ArrayAdapter<String> { private static List<String> mClubs = null; public TextView tv; public ClubsAdapter(Context context, int textViewResour
微调器中显示的第一个项目上有一个特定的文本外观。
尽管调用并执行了下面的代码,但微调器中的第一项并没有按预期格式化
public class ClubsAdapter extends ArrayAdapter<String> {
private static List<String> mClubs = null;
public TextView tv;
public ClubsAdapter(Context context, int textViewResourceId, List<String> dataset) {
super(context, textViewResourceId, dataset);
mClubs = dataset;
ListIterator<String> listIterator = mClubs.listIterator();
String s = listIterator.next();
listIterator.set(s.substring(1));
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater =
(LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.club, null);
}
tv = v.findViewById(R.id.club);
String club = mClubs.get(position);
if (club != null) {
if (position == 0) {
tv.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
tv.setTextAppearance(android.R.style.TextAppearance_Medium);
} else {
tv.setTextAppearance(mContext, android.R.style.TextAppearance_Medium);
}
} else {
tv.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
tv.setTextAppearance(android.R.style.TextAppearance_Large);
} else {
tv.setTextAppearance(mContext, android.R.style.TextAppearance_Large);
}
}
tv.setText(club);
}
return v;
}
公共类ClubAdapter扩展了ArrayAdapter{
私有静态列表mClubs=null;
公共图文电视;
公共ClubsAdapter(上下文上下文、int textViewResourceId、列表数据集){
super(上下文、textViewResourceId、数据集);
mClubs=数据集;
ListIterator ListIterator=mClubs.ListIterator();
字符串s=listIterator.next();
set(s.substring(1));
}
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
如果(v==null){
充气机=
(LayoutFlater)getContext().getSystemService(Context.LAYOUT\u充气机\u服务);
v=充气机充气(R.layout.club,空);
}
电视=v.findviewbyd(R.id.club);
弦杆=mClubs.get(位置);
如果(俱乐部!=null){
如果(位置==0){
tv.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC));
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.M){
tv.settextearance(android.R.style.textpearance_Medium);
}否则{
tv.settext外观(mContext、android.R.style.TextAppearance\u Medium);
}
}否则{
设置字体(Typeface.defaultFromStyle(Typeface.NORMAL));
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.M){
tv.settextearance(android.R.style.textpearance\u Large);
}否则{
tv.settext外观(mContext、android.R.style.TextAppearance\u大号);
}
}
tv.setText(俱乐部);
}
返回v;
}
}
以下是XML:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/club"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
这就是我如何使用这个适配器:
Spinner clubSpinner = (Spinner) root.findViewById(R.id.clubSpinner);
ArrayAdapter<String> clubsAdapter = new ClubsAdapter(getContext(), R.layout.club, clubNames);
clubsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
clubSpinner.setAdapter(clubsAdapter);
Spinner clubsinner=(Spinner)root.findviewbyd(R.id.clubsinner);
ArrayAdapter clubsAdapter=新的clubsAdapter(getContext(),R.layout.club,clubNames);
clubAdapter.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
clubsinner.setAdapter(clubAdapter);
***编辑***
根据请求,这是微调器的XML:
<Spinner android:id="@+id/clubSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@id/courseSpinner"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
在设置外观
之后移动设置字体
。后者还设置类型面。
简单而愚蠢
正确的代码是
if (position == 0) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
tv.setTextAppearance(android.R.style.TextAppearance_Medium);
} else {
tv.setTextAppearance(mContext, android.R.style.TextAppearance_Medium);
}
tv.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC));
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
tv.setTextAppearance(android.R.style.TextAppearance_Large);
} else {
tv.setTextAppearance(mContext, android.R.style.TextAppearance_Large);
}
tv.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
}
你能把spinner XML也贴出来吗?我需要完整的代码来帮助你。或者更重要的是出现问题的那一部分。问题如下:当我使用调试器时,我看到“getview”方法中的“settypeface”和“settextappearance”被执行,但不会影响显示的结果。看起来像是其他一些方法稍后会覆盖/回滚它们。