簡體   English   中英

Android微調器更改特定項目的背景顏色

[英]android spinner change the background color for specific items

我有一個微調器,我想更改特定項目的背景顏色。 我的意思是,如果該對象被命名為:country,並且其值為section=true ,則微調器的背景color = blue將為color = blue

@Override
   public View getView(int position, View convertView, ViewGroup parent) {
 final kamcoReportType report = values.get(position);
  if (convertView == null) {
    Context mContext = this.getContext();
   LayoutInflater vi = (LayoutInflater)   mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    TextView tv=(TextView) convertView.findViewById(R.id.TextView01);
    if (report.isSection())
       tv.setBackgroundColor=blue  //what ever

我怎樣才能做到這一點。

在您的getView()方法上,您可以執行以下操作:

        if (yourCondition) {
            tv.setBackgroundColor(getContext().getResources().getColor(R.color.blue));
        }

這應該可以解決問題。

@更新

如果我說對了,您想在getView()方法中獲取當前項目,對嗎?

我假設您的適配器正在擴展ArrayAdapter<T> 在這種情況下,您可以使用ArrayAdapter getItem(position)方法獲取項目並進行如下測試:

if ((getItem(position)).isSection()) { }

在您的getView()內部。

希望這可以幫助。

嘗試這個:

if (report.isSection())
   tv.setBackgroundColor(Color.parseColor("#0D47A1"));

閱讀您的問題,我知道您想訪問列表中的項目。 好吧,這是您可以做到的:

   @Override
   public View getView(int position, View convertView, ViewGroup   parent) {
     final kamcoReportType report = values.get(position);
     if (convertView == null) {
         Context mContext = this.getContext();
         LayoutInflater vi = (LayoutInflater)   mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         TextView tv=(TextView) convertView.findViewById(R.id.TextView01);
         if (report.isSection())
         tv.setBackgroundColor(getContext().getResources().getColor(R.color.blue));
     //here is how will you access your list: let say, your data list name is "itemList"

        item=itemList.get(position);//it will return the item from that list



}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM