只更改一个项目微调器的样式。

huangapple go评论87阅读模式
英文:

Change style of only one item spinner android

问题

我定义了一个类似这样的下拉框,带有自己的适配器。
如何仅更改下拉框中的一个项目样式?
特别地,我想要改变下拉框中插入的最后一个字符串的颜色。
谢谢

Spinner spinner = findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, "List<String>");
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
英文:

I defined a spinner like this, with its own adapter.
How can I change the style of only one item of the spinner?
In particular I would like to change the color of the last string inserted in the spinner.
Thanks

Spinner spinner = findViewById(R.id.spinner);
ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;&gt;(this, android.R.layout.simple_spinner_item, &quot;List&lt;String&gt;&quot;);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

答案1

得分: 1

你可以为Spinner创建自定义适配器,在适配器的getView方法中添加条件,当位置是最后一个时,更改视图(TextView)的背景颜色。

更新:

创建CustomAdapter:

public class CustomAdapter extends BaseAdapter {
    private final Context context;
    private final List<String> list;

    public CustomAdapter(@NonNull Context context, @NonNull List<String> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View view;
        if (convertView == null) {
            view = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
        } else {
            view = convertView;
        }

        TextView textView = view.findViewById(R.id.textView_name);
        textView.setText(list.get(position));
        if (position == list.size() - 1) {
            textView.setBackgroundResource(android.R.color.holo_blue_light);
        }
        return view;
    }
}

list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:id="@+id/textView_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:gravity="center"
        android:textStyle="bold"
       />
</androidx.constraintlayout.widget.ConstraintLayout>

然后将CustomAdapter设置给Spinner:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    List<String> list = new ArrayList<String>();
    list.add("first");
    list.add("second");
    list.add("third");
    list.add("forth");
    Spinner spinner = findViewById(R.id.spinner);
    CustomAdapter adapter = new CustomAdapter(this, list);
    spinner.setAdapter(adapter);
}

结果将如下图所示:

只更改一个项目微调器的样式。

英文:

You can create custom adapter for spinner, in adapter inside getView add a condition when a position is the last one, change a view(TextView) background color.

UPDATE :

create customAdapter

public class CustomAdapter extends BaseAdapter {
private final Context context;
private final List&lt;String&gt; list;
public CustomAdapter(@NonNull Context context, @NonNull List&lt;String&gt; list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view;
if (convertView == null) {
view = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
} else {
view = convertView;
}
TextView textView = view.findViewById(R.id.textView_name);
textView.setText(list.get(position));
if (position == list.size() - 1) {
textView.setBackgroundResource(android.R.color.holo_blue_light);
}
return view;
}
}

list_item.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;&gt;
&lt;TextView
android:id=&quot;@+id/textView_name&quot;
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:textSize=&quot;20sp&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
android:gravity=&quot;center&quot;
android:textStyle=&quot;bold&quot;
/&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

then set customAdpater to spinner

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List&lt;String&gt; list = new ArrayList&lt;String&gt;();
list.add(&quot;first&quot;);
list.add(&quot;second&quot;);
list.add(&quot;third&quot;);
list.add(&quot;forth&quot;);
Spinner spinner = findViewById(R.id.spinner);
CustomAdapter adapter = new CustomAdapter(this, list);
spinner.setAdapter(adapter);
}

result will be as the photo

只更改一个项目微调器的样式。

huangapple
  • 本文由 发表于 2020年9月19日 03:14:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63961582.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定