代码之家  ›  专栏  ›  技术社区  ›  Mido2009

使用多行填充微调器项

  •  5
  • Mido2009  · 技术社区  · 7 年前

    微调器中的某些项目很长,我希望微调器根据文本的长度将文本包装到多行上。为此,我查看了几种解决方案,但它们对我不起作用。

    此处显示不合适的文本:

    enter image description here

    以下是我的Java代码:

    public void addItemsOnSpinner3() {
    
            spinner3D = (Spinner) findViewById(R.id.activities1);
            List<String> list = new ArrayList<String>();
            list.add("Academic Year Opening Program");
            list.add("Academic Year Closing Program");
            list.add("LR1: Defining Primary Care, Health Care Disparities & Vulnerable Populations");
            list.add("LR2: Community Resources and the Elderly");
            list.add("LR3: Inter-professional Teams and the Homeless");
            list.add("LR4: Quality Improvement and Veterans");
            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                    R.layout.multiline_spinner_row, list);
            dataAdapter.setDropDownViewResource(R.layout.multiline_spinner_row);
            spinner3D.setAdapter(dataAdapter);
        }
    

    我创建了一个名为“multiline\u spinner\u row.xml”的自定义行,其中singleline更改为false:

    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:singleLine="false"
        xmlns:android="http://schemas.android.com/apk/res/android" />
    

    这只会使我的选择换行,但不会换行:

    enter image description here

    任何帮助都将不胜感激!提前感谢!

    4 回复  |  直到 4 年前
        1
  •  4
  •   Amani    7 年前

    在微调器中设置此属性:

    android:spinnerMode="dialog"
    

    在文本视图中设置以下属性:

    android:layout_weight="1"  
    android:ellipsize="none"  
    android:maxLines="100"  
    android:scrollHorizontally="false"
    
        2
  •  2
  •   AbsoluteBeginner    4 年前

    这实际上对我有用(在Xamarin Android中,但问题是一样的):

    而不是

    adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
    

    adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleExpandableListItem1);
    
        3
  •  1
  •   a522510162    4 年前

    创建这样一个xml文件,您的xml需要有一个父布局来包装TextView。

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    
    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textColor="#333333"
        android:textSize="15sp"
        tools:text="123" />
    
    </RelativeLayout>
    

    确保TextView具有id,然后像这样创建适配器。

    private ArrayAdapter<CharSequence> stringAdapter = new ArrayAdapter<>(context, R.layout.spinner_textview, R.id.textView, displayTexts);
    

    使用此构造函数

    public ArrayAdapter(@NonNull Context context, @LayoutRes int resource,
            @IdRes int textViewResourceId, @NonNull List<T> objects) {
        this(context, resource, textViewResourceId, objects, false);
    }
    

    或将textViewResourceId作为参数的任何构造函数

    就是这样。您不需要覆盖任何其他内容。 查看下图以查看效果。 enter image description here enter image description here

        4
  •  0
  •   Waruna Lakshantha    4 年前

    试试这个

    我用简单的编码解决了这个问题。

    只需添加Android即可。资源布局为微调器分配适配器时,SimpleExpandableListItem1如下所示。

    请查看更多 https://notostuck.blogspot.com/2020/08/how-to-populating-spinner-items-with.html