开发者

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

开发者 https://www.devze.com 2023-03-27 04:54 出处:网络
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 s开发者_如何学JAVAcrolling text on the Spinner?For spinner, you have to crea

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 s开发者_如何学JAVAcrolling text on the Spinner?


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" />


you have to create some custom spinner

Adapter.setDropDownViewResource(R.layout.spinner);


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" />
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号