我有一些项目的微调器,其中一些有很长的文本

发布于 2024-11-28 23:49:26 字数 60 浏览 1 评论 0原文

我有带有一些物品的微调器。有些项目的文本很长,因此不会出现在微调器上。如何在 Spinner 上滚动文本?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泡沫很甜 2024-12-05 23:49:26

对于微调器,您必须创建 xml 文件

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

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" />
蓝眼泪 2024-12-05 23:49:26

您必须创建一些自定义微调器

Adapter.setDropDownViewResource(R.layout.spinner);

you have to create some custom spinner

Adapter.setDropDownViewResource(R.layout.spinner);

×眷恋的温暖 2024-12-05 23:49:26

您必须

在 Activity 中的

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

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 类

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

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" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文