在android中手动设置微调下拉列表的宽度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1551951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:14:24  来源:igfitidea点击:

Manually set width of spinner dropdown list in android

androidlayoutspinner

提问by aspartame

Is it possible to set the width of a spinner dropdown list in code? I have a spinner populated with integers, and it does not look good with the list expanding to full width. Can I set the width to wrap the content somehow?

是否可以在代码中设置微调下拉列表的宽度?我有一个填充了整数的微调器,并且列表扩展到全宽时看起来不太好。我可以设置宽度以某种方式包装内容吗?

Spinner hSpinner = (Spinner) timerView.findViewById(R.id.timer_hour_spinner);
ArrayList<Integer> hList = new ArrayList<Integer>(21);

for (int i = 0; i <= 20; i++) { hList.add(i); }

ArrayAdapter hAdapter = new ArrayAdapter(RemindMe.this, android.R.layout.simple_spinner_item, hList);
hAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

hSpinner.setAdapter(hAdapter);

Thanks!

谢谢!

Linus

莱纳斯

采纳答案by CommonsWare

You can change the width of anything in code by adjusting its LayoutParams. The details of that varies by container (LinearLayoutvs. RelativeLayoutvs. ...).

您可以通过调整代码中的任何内容来更改其宽度LayoutParams。详细信息因容器而异(LinearLayoutvs. RelativeLayoutvs. ...)。

However, I'm confused why you want to change the width "in code". Why not just set the width to be wrap_contentin the layout XML?

但是,我很困惑为什么要“在代码中”更改宽度。为什么不将宽度设置wrap_content在布局 XML 中?

回答by JPMagalhaes

Just use:

只需使用:

setDropDownWidth(desiredWidth);

回答by PeteH

It is straightforward to control the spinner dropdown appearance. Try this:

控制微调器下拉外观很简单。尝试这个:

//Step 1. create the drop down list
static List<String> special_Spinner_Display_List = new ArrayList<String>();  
// add your values to the list...(this is best done using a for loop)
special_Spinner_Display_List.add(item1);
special_Spinner_Display_List.add(item2);  //etc., etc.

//Step 2. build the spinner
ArrayAdapter arrayAdapter_special = new ArrayAdapter(this, 
      R.layout.your_special_spinner, special_Spinner_Display_List);
arrayAdapter_special.setDropDownViewResource(R.layout.special_spinner_dropdown);
specialSpinner.setAdapter(arrayAdapter_special);

//Step 3. create an XML layout file called special_spinner_dropdown where you can 
//style to your heart's content.  Here's an example:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/SpinnerDropdown"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#D5ECED"
android:ellipsize="marquee"
android:singleLine="true"
android:textSize="24sp" 
android:textStyle="bold"
android:textColor="@color/black" />

That's it. Lemme know how it works!

就是这样。让我知道它是如何工作的!

回答by abhishesh

Set dropdown width in xml file of spinner using the tag

使用标签在微调器的 xml 文件中设置下拉宽度

android:dropDownWidth="@dimen/desired_width"

android:dropDownWidth="@dimen/desired_width"

Explaination :

说明:

mDropDownWidth = pa.getLayoutDimension(R.styleable.Spinner_dropDownWidth,
        ViewGroup.LayoutParams.WRAP_CONTENT);

this field is used for dropdown width when spinner item is initialized in Spinner class

在 Spinner 类中初始化微调项时,此字段用于下拉宽度

for programatically changing spinner dropdown width above api level 16, use

要以编程方式更改 api 级别 16 以上的微调下拉菜单宽度,请使用

mSpinner.setDropDownWidth(size_in_pixel);