android-如何使微调框宽度与最大可用项目宽度相同
我意识到甚至我都将UnitedKingdom用于微调器(我避免使用match_parent,因为我不想让微调器过长),并且将match_parent用于微调器的项目视图和下拉项目视图,
我仍然发现微调框的宽度与最大项目的宽度不同
如您所见,UnitedKingdom不适合。
因此,如果我选择UnitedKingdom,则只有微调框会调整大小
即使没有明确选择最大的项目,如何使微调框调整为最大可用项目宽度?
我的微调器XML是
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp" />
我微调器的视图XML是
android:id="@+id/text_view_0"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawablePadding="10dp"
android:gravity="center_vertical" />
android:id="@+id/checked_text_view_0"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="48dp"
android:minHeight="48dp"
android:drawablePadding="10dp" />
Cheok Yan Cheng asked 2020-01-24T19:25:03Z
6个解决方案
16 votes
通过将下拉菜单的宽度指定为WRAP_CONTENT,您应该能够获得所需的效果:
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:dropDownWidth="wrap_content"
android:spinnerMode="dropdown" />
这将在微调器的适配器中最多测量15个项目,以确定其应使用的宽度。
Ted Hopp answered 2020-01-24T19:25:27Z
8 votes
我只是通过使用“解决”问题
android:id="@+id/country_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="200dp"
android:layout_marginBottom="10dp" />
200dp只是try-n-error值,以确保下拉列表中的所有项目均可见。
Cheok Yan Cheng answered 2020-01-24T19:25:51Z
0 votes
将您的TextView放入Spinner Item XML的LineanLayout中,
android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:id="@+id/text_view_0"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawablePadding="10dp"
android:gravity="center_vertical" />
并将您的TextView的ID传递给自定义适配器构造函数
CustomAdapter adapter = new CustomAdapter(this,R.layout.spinner_item,R.id.text_view_0,arraylist);
建设者
public CustomAdapter(Context context, int resource, int textViewId, ArrayList resultList) {
super(context, resource, textViewId, resultList);
...
...
}
Vicky answered 2020-01-24T19:26:20Z
0 votes
将宽度设置为0dp,布局权重为1。以下内容将为您调整大小。
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="?android:attr/actionBarSize"
android:gravity="center">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/test label"/>
android:id="@+id/test_spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:dropDownWidth="wrap_content"
android:layout_weight="1"
android:prompt="@string/test_prompt"
android:spinnerMode="dropdown" />
Calysmic answered 2020-01-24T19:26:40Z
0 votes
在这种情况下,您想列出所有国家或拥有大名单
尝试:spinnerMode =“ dialog”如下所示:
android:id="@+id/spinnerId"
android:layout_width="wrap_content"
android:dropDownWidth="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:spinnerMode="dialog"/>
Sujay U N answered 2020-01-24T19:27:04Z
-7 votes
试试这个:android:layout_width="fill_parent"
Mahmoud Abdulfatah answered 2020-01-24T19:27:24Z