I have Spinner with some items, some of which have long text

拜拜、爱过 提交于 2019-12-25 03:57:07

问题


I have Spinner with some items. Some of the items are having long text, so its not appearing on the spinner. How can I have scrolling text on the Spinner?


回答1:


For spinner, you have to create xml file

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/text1"
style="android:attr/dropDownItemStyle"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="45px"
android:ellipsize="marquee"
 android:textColor="#000000"
android:gravity="center_vertical" />



回答2:


you have to create some custom spinner

Adapter.setDropDownViewResource(R.layout.spinner);




回答3:


you have to create a custom spinner

Globals in Activity

String[] spinnerValues = { "1-10", "10-100", "100-200","200-500", "500-1000","1000-2000","2000-5000","No. of Employees" };
 Private Spinner _spin;

In oncreate

_spin= (Spinner) findViewById(R.id.your_spinner_id);
_spin.setAdapter(new MyAdapter(this,R.layout.inflator_file,spinnerValues)); 
_spin.setSelection(spinnerValues.length - 1); // used to set a prompt in dropdown spinner.

Adapter class

public class MyAdapter extends ArrayAdapter<String> {

    public MyAdapter(Context ctx, int txtViewResourceId, String[] objects) {
        super(ctx, txtViewResourceId, objects);
    }

    @Override
    public View getDropDownView(int position, View cnvtView, ViewGroup prnt) {
        return getCustomView(position, cnvtView, prnt);
    }
    @Override
    public View getView(int pos, View cnvtView, ViewGroup prnt) {
        return getCustomView(pos, cnvtView, prnt);
    }
    public View getCustomView(int position, View convertView,ViewGroup parent) {
        LayoutInflater inflater = getLayoutInflater();
        View mySpinner = inflater.inflate(R.layout.inflator_file, parent,false);
        TextView main_text = (TextView) mySpinner.findViewById(R.id.textone);
        main_text.setText(spinnerValues[position]);
        return mySpinner;
    }
}

inflater_file.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/textone"   
android:singleLine="true"
android:textColor="#9c9a9b"
android:gravity="left|center"
android:typeface="serif"
android:paddingLeft="8dp"
android:textSize="14sp"
android:layout_width="fill_parent"
android:layout_height="24dp"   
android:ellipsize="marquee" />


来源:https://stackoverflow.com/questions/7009398/i-have-spinner-with-some-items-some-of-which-have-long-text

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!