用多行填充微调项
populating a spinner items with multiple lines
我的微调器中的某些项目很长,我希望微调器根据文本的长度将文本换行成多行。我为此查看了 serval 解决方案,但它们不适合我。
这里显示的文字不合适:
这是我在 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_spinner_row.xml”的自定义行,其中单行更改为 false:
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="false"
xmlns:android="http://schemas.android.com/apk/res/android" />
所有这一切都是让我的选择环绕文本而不是项目:
如有任何帮助,我们将不胜感激!提前致谢!
在微调器中设置这个 属性:
android:spinnerMode="dialog"
在文本视图中设置这些属性:
android:layout_weight="1"
android:ellipsize="none"
android:maxLines="100"
android:scrollHorizontally="false"
试试这个
我用简单的代码解决了这个问题。
只需添加 Android.Resource.Layout.SimpleExpandableListItem1 即可为 Spinner 分配适配器,如下所示。
请查看更多
https://notostuck.blogspot.com/2020/08/how-to-populating-spinner-items-with.html
这实际上对我有用(在 Xamarin Android 中,但问题是一样的):
而不是
adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
写
adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleExpandableListItem1);
像这样创建一个 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
我的微调器中的某些项目很长,我希望微调器根据文本的长度将文本换行成多行。我为此查看了 serval 解决方案,但它们不适合我。
这里显示的文字不合适:
这是我在 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_spinner_row.xml”的自定义行,其中单行更改为 false:
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="false"
xmlns:android="http://schemas.android.com/apk/res/android" />
所有这一切都是让我的选择环绕文本而不是项目:
如有任何帮助,我们将不胜感激!提前致谢!
在微调器中设置这个 属性:
android:spinnerMode="dialog"
在文本视图中设置这些属性:
android:layout_weight="1"
android:ellipsize="none"
android:maxLines="100"
android:scrollHorizontally="false"
试试这个
我用简单的代码解决了这个问题。
只需添加 Android.Resource.Layout.SimpleExpandableListItem1 即可为 Spinner 分配适配器,如下所示。
请查看更多 https://notostuck.blogspot.com/2020/08/how-to-populating-spinner-items-with.html
这实际上对我有用(在 Xamarin Android 中,但问题是一样的):
而不是
adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
写
adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleExpandableListItem1);
像这样创建一个 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