Android 使用多行填充微调器项
我的微调器中的某些项目很长,我希望微调器根据文本的长度将文本环绕多行。为此,我查看了一些解决方案,但它们对我不起作用 此处显示不合适的文本: 以下是我的Java代码:Android 使用多行填充微调器项,android,android-layout,android-spinner,Android,Android Layout,Android Spinner,我的微调器中的某些项目很长,我希望微调器根据文本的长度将文本环绕多行。为此,我查看了一些解决方案,但它们对我不起作用 此处显示不合适的文本: 以下是我的Java代码: public void addItemsOnSpinner3() { spinner3D = (Spinner) findViewById(R.id.activities1); List<String> list = new ArrayList<String>();
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);
}
public void addItemsOnSpinner3(){
喷丝头3D=(喷丝头)findViewById(R.id.activities1);
列表=新的ArrayList();
列表。添加(“学年开学计划”);
列表。添加(“学年结束计划”);
列表。添加(“LR1:定义初级保健、保健差异和弱势人群”);
列表。添加(“LR2:社区资源和老年人”);
列表。添加(“LR3:跨专业团队和无家可归者”);
列表。添加(“LR4:质量改进和退伍军人”);
ArrayAdapter dataAdapter=新的ArrayAdapter(此,
R.layout.multiline_微调器_行,列表);
dataAdapter.setDropDownViewResource(R.layout.multiline\u微调器\u行);
喷丝头3D.setAdapter(数据适配器);
}
我创建了一个名为“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" />
所有这一切只是使我的选择包装文本,而不是项目:
任何帮助都将不胜感激!提前谢谢 在微调器中设置此属性:
android:spinnerMode="dialog"
在文本视图中设置以下属性:
android:layout_weight="1"
android:ellipsize="none"
android:maxLines="100"
android:scrollHorizontally="false"
试试这个 我用简单的编码解决了这个问题 在为微调器分配适配器时,只需添加Android.Resource.Layout.SimpleExpandableListItem1,如下所示 请查看更多信息
这实际上对我有效(在Xamarin Android中,但问题是相同的): 而不是
adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
写
创建这样一个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>
我们需要的是下拉列表,而不是弹出列表对话框。请详细说明一下,尽量避免在答案中粘贴简单的URL。
<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>
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);
}