【问题标题】:Manually set width of spinner dropdown list in android在android中手动设置微调器下拉列表的宽度
【发布时间】:2010-12-05 20:02:38
【问题描述】:

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

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);

谢谢!

莱纳斯

【问题讨论】:

    标签: android layout spinner


    【解决方案1】:

    只需使用:

    setDropDownWidth(desiredWidth);
    

    【讨论】:

      【解决方案2】:

      您可以通过调整LayoutParams 来更改代码中任何内容的宽度。具体细节因容器而异(LinearLayout vs. RelativeLayout vs. ...)。

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

      【讨论】:

      • 我在布局文件中将微调器的宽度设置为“wrap_content”,但是当我在应用程序中单击微调器时,下拉列表会扩展为全宽。这就是为什么我认为如果可能的话必须在代码中调整它的大小。我的问题可能不清楚,但我要设置宽度的是下拉列表,而不是微调器对象本身。有任何想法吗?一如既往,感谢您的帮助!
      • 啊,抱歉,误会了。您可以为下拉视图尝试不同的布局。我不知道宽度是由下拉布局定义的,还是“下拉”对话框固有的。如果是后者,你可能无能为力。
      • 好吧,我想如果可能的话它会很混乱。我实际上跳过了微调器并转而使用了 SeekBar。看起来不错,所以还是成功了!感谢您的帮助!
      【解决方案3】:

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

      //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" />
      

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

      【讨论】:

      • 你知道如何设置下拉资源的宽度吗?它似乎不起作用。
      【解决方案4】:

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

      android:dropDownWidth="@dimen/desired_width"

      说明:

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

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

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

      mSpinner.setDropDownWidth(size_in_pixel);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-12-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-16
        • 2014-02-20
        • 1970-01-01
        相关资源
        最近更新 更多