android 可变宽度区域,android-如何使微调框宽度与最大可用项目宽度相同

android-如何使微调框宽度与最大可用项目宽度相同

我意识到甚至我都将UnitedKingdom用于微调器(我避免使用match_parent,因为我不想让微调器过长),并且将match_parent用于微调器的项目视图和下拉项目视图,

我仍然发现微调框的宽度与最大项目的宽度不同

3ae6d9198b541a3c499bcca951304223.png

如您所见,UnitedKingdom不适合。

因此,如果我选择UnitedKingdom,则只有微调框会调整大小

20ff2e7108da288b1af3e2cb098bf4d9.png

即使没有明确选择最大的项目,如何使微调框调整为最大可用项目宽度?

我的微调器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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值