【问题标题】:How can I style android spinner prompt如何设置 android spinner 提示样式
【发布时间】:2012-10-16 11:27:58
【问题描述】:

我开发了一个例子。

在这里,我必须设置微调器提示的样式。请帮我。我该如何设计它?

这是我的 string.xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, CustomizedListView!</string>
<string name="app_name">CustomizedListView</string>
<string name="status_prompt">Choose a Status</string>
</resources>

此代码用于我的 main.xml:

<Spinner android:id="@+id/spinner1" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_x="106dp"
    android:layout_y="100dp"
    android:prompt="@string/status_prompt" 
    android:background="@drawable/btn_dropdown"/>

这里我想改变背景颜色和textSizetextStyle在下图。我该如何改变这个。

编辑: 我在 styles.xml

中添加了以下代码
<style name="spinner_style">                        
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#040404</item>
<item name="android:typeface">monospace</item>
<item name="android:background">#FFFFFF</item>
</style>

还在 main.xml 中为微调器添加了以下行:

  style="@style/spinner_style"   

但这对我也不起作用。如何设置微调器提示消息的样式。

编辑:

这是我的 ArrayAdaper 的 java 代码:

 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, list);
    //set the view for the Drop down list
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    //set the ArrayAdapter to the spinner
    spinner.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    //attach the listener to the spinner
    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

    }

这里如何设置提示样式。

【问题讨论】:

    标签: android android-spinner prompt


    【解决方案1】:

    为了更改提示,您需要使用与@aswin-kumar 建议的方法类似的方法,但对下拉列表的第一个元素使用单独的样式:

    首先,扩展ArrayAdapter 并将其命名为CustomArrayAdapter: 包 com.example.project;

    package com.example.project;
    
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    
    import java.util.List;
    
    
    // I extend this using ArrayAdapter<String>, but you can use anything as the type
    // parameter, or parameterize it as necessary.
    public class CustomArrayAdapter extends ArrayAdapter<String> {
    
        private String title;
    
        public CustomArrayAdapter(Context aContext, int aTextViewResource, List<String> aOptions, String title) {
            super(aContext, aTextViewResource, aOptions);
            this.title = title;
        }
    
        @Override
        public View getDropDownView(int aPosition, View aConvertView, ViewGroup aParent) {
    
            LinearLayout v = null;
    
            if (aPosition == 0) {
                LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = (LinearLayout) inflater.inflate(R.layout.dropdown_header, aParent, false);
                TextView tv = (TextView) v.findViewById(R.id.dropdown_item);
                tv.setText(getItem(aPosition));
            } else {
                LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = (LinearLayout) inflater.inflate(R.layout.dropdown_item, aParent, false);
                TextView tv = (TextView) v.findViewById(R.id.dropdown_item);
                tv.setText(getItem(aPosition));
                tv.setHeight((int) (tv.getTextSize() * 2));
            }
    
    
            return v;
        }
    
        @Override
        public int getCount() {
            return super.getCount() + 1;
        }
    
        @Override
        public String getItem(int position) {
            if (position == 0) {
                return title;
            }
            return super.getItem(position - 1);
        }
    
        @Override
        public boolean isEnabled(int position) {
            return position != 0;
        }
    }
    

    现在,为您的dropdown_header.xml 使用以下布局:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
        <TextView
            android:id="@+id/dropdown_item"
            android:textColor="@android:color/black"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="16dp"
            android:paddingBottom="16dp"
            android:paddingLeft="16dp"
            android:gravity="left|center_vertical"
            android:textSize="9pt" />
    
        <!-- This adds a black separator line between the title and the items. You can remove
             if you want -->
        <LinearLayout
            android:id="@+id/separator"
            android:layout_height="4dp"
            android:layout_width="match_parent"
            android:background="@android:color/black"
            android:orientation="vertical"/>
        </LinearLayout>
    

    表单视图布局文件(form_view_layout.xml):

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/spinner_dialog_root"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="9pt" />
    

    微调器布局文件 (spinner_layout.xml):

     <Spinner
    android:id="@+id/spinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="@android:color/transparent"/>
    

    现在,将新适配器连接到您的 Spinner

    CustomArrayAdapter adapter = new CustomArrayAdapter(aActivity, R.layout.form_view_layout, aModel.getEnumerableOptions(), aModel.getTitle());
    View parentView = aActivity.getLayoutInflater().inflate(R.layout.spinner_layout, aParent, false);
    
    spinner = (Spinner) parentView.findViewById(R.id.spinner);
    spinner.setAdapter(adapter);
    

    最后,将要显示的字符串添加到微调器适配器adapter,并设置适当文件的样式,然后就可以开始了。

    【讨论】:

      【解决方案2】:

      您需要为从ArrayAdapter 扩展的微调器创建自己的适配器。 getView() 函数在布局系统指示您在 pic 中显示的视图需要绘制时被调用。您可以覆盖它并添加 textSize、textStyle 等。

      更新:粗略的代码示例是:

      private class MySpinnerAdapter extends ArrayAdapter<String> {
              public MySpinnerAdapter(Context context, int resource,
                      int textViewResourceId, List<String> objects) {
                  super(context, resource, textViewResourceId, objects);
              }
      
          public View getView(int position, View convertView,
                  ViewGroup parent) {
              View v = super.getView(position, convertView, parent);
              // apply the style and sizes etc to the Text view from this view v
              // like ((TextView)v).setTextSize(...) etc
              return v;
          }
      
          public View getDropDownView(int position, View convertView,
                  ViewGroup parent) {
              // this is for each of the drop down resource that is created. 
                  // you can style these things too
              }
      }
      

      将其用作微调器的适配器:

      mySpinner.setAdapter(new MySpinnerAdapter(ActivityName.this,
                          R.layout.spinner_item,
                          R.id.spinner_item_TextView, 
                          listOfItemsInSpinner));
      

      【讨论】:

      • 在上面的代码中仅更改微调器项目的样式。我希望更改微调器提示样式。
      猜你喜欢
      • 1970-01-01
      • 2011-09-19
      • 2012-03-14
      • 2015-03-20
      • 2016-08-14
      • 1970-01-01
      • 1970-01-01
      • 2016-10-01
      • 2011-09-29
      相关资源
      最近更新 更多