【问题标题】:Android: Change Spinner Dropdown viewAndroid:更改微调器下拉视图
【发布时间】:2012-11-26 07:29:13
【问题描述】:

我是我的应用程序,我想要以下类型的 Spinner 下拉视图。 对于这种类型的微调器视图。我写了这段代码。

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.spinner, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner_obj.setAdapter(adapter);

我从http://developer.android.com/guide/topics/ui/controls/spinner.html 得到这个 但我得到的是,

请给我最好的方法来做到这一点......

【问题讨论】:

  • 你找到解决这个问题的办法了吗?
  • 有答案了吗?我有同样的问题。
  • 你在哪个 api 上测试它

标签: android android-layout spinner android-spinner


【解决方案1】:

有点像在这里恢复旧帖子,但接受的答案远非理想。正确的做法是在布局 xml 中将 Spinner 设置为下拉模式:

<Spinner 
    android:id="@+id/my_spinner"
    ...
    android:spinnerMode="dropdown"/>

可用的选项是“对话框”和“下拉菜单”。

【讨论】:

  • 是的,它与主题无关......只需在任何版本的android中使用此属性!
  • No resource identifier found for attribute 'spinnerMode' in package 'android',现在呢?旧版本不可用?
  • 同样的问题,不改
【解决方案2】:

您的应用程序正在旧主题上运行。

如果您使用的是 android 4.2,请将 android 应用程序主题(在清单文件中)设置为

 android:theme="@android:style/Theme.Holo.Light"

 android:theme="@android:style/Theme.Holo.Light.DarkActionBar"

【讨论】:

  • 这让我的应用程序崩溃了:/
  • 这里的崩溃可能是因为Actionbar和Toolbar设置冲突(如果使用toolbar请选择非actionbar主题)。
【解决方案3】:

可能你在低于 4.0 的环境下运行,4.0 会显示下拉菜单作为你的图片

【讨论】:

【解决方案4】:

您可以使用如下弹出窗口:

           spinner=(EditText)findViewById(R.id.txt_Spinner);


        spinner.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                p = new Point();
                p.x = location[0]+(v.getHeight());
                p.y = location[1]+v.getHeight();

                if (p != null)
                    showPopup(statusActivity.this, p);

                System.out.println("show popup");
            }
        });




    // The method that displays the popup.
    private void showPopup(final Activity context, Point p) {
        int popupWidth = 300;
        int popupHeight = 500;

        // Inflate the popup_layout.xml
        LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
        LayoutInflater layoutInflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);

        // Creating the PopupWindow
        popup = new PopupWindow(context);
        popup.setContentView(layout);
        popup.setWidth(popupWidth);
        popup.setHeight(popupHeight);
        popup.setFocusable(true);

        // Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
        int OFFSET_X = 00;
        int OFFSET_Y = 00;

        // Clear the default translucent background
        popup.setBackgroundDrawable(new BitmapDrawable());

        // Displaying the popup at the specified location, + offsets.
        popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
        ((TextView)layout.findViewById(R.id.textView2)).setClickable(true);
        ((TextView)layout.findViewById(R.id.textView3)).setClickable(true);
        ((TextView)layout.findViewById(R.id.textView4)).setClickable(true);
        ((TextView)layout.findViewById(R.id.textView5)).setClickable(true);
        ((TextView)layout.findViewById(R.id.textView6)).setClickable(true);
        ((TextView)layout.findViewById(R.id.textView7)).setClickable(true);
        ((TextView)layout.findViewById(R.id.textView8)).setClickable(true);
        ((TextView)layout.findViewById(R.id.textView9)).setClickable(true);

    }

and popup.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/popup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/popup_bg"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        style="@style/text_orange_heading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select Status"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        style="@style/text_blue_contains"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:onClick="onClick"
        android:clickable="true"
        android:drawableBottom="@drawable/line_white"
        android:tag="Sleeping"
        android:text="Sleeping" />

    <TextView
        android:id="@+id/textView3"
        style="@style/text_blue_contains"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:onClick="onClick"
        android:clickable="true"
        android:drawableBottom="@drawable/line_white"
        android:tag="Available"
        android:text="Available" />

    <TextView
        android:id="@+id/textView4"
        style="@style/text_blue_contains"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:onClick="onClick"
        android:clickable="true"
        android:drawableBottom="@drawable/line_white"
        android:tag="Busy"
        android:text="Busy" />

    <TextView
        android:id="@+id/textView5"
        style="@style/text_blue_contains"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:onClick="onClick"
        android:clickable="true"
        android:drawableBottom="@drawable/line_white"
        android:tag="At work"
        android:text="At work" />

    <TextView
        android:id="@+id/textView6"
        style="@style/text_blue_contains"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:onClick="onClick"
        android:clickable="true"
        android:drawableBottom="@drawable/line_white"
        android:tag="Battery charge low"
        android:text="Battery charge low" />

    <TextView
        android:id="@+id/textView7"
        style="@style/text_blue_contains"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:onClick="onClick"
        android:clickable="true"
        android:drawableBottom="@drawable/line_white"
        android:tag="In meeting"
        android:text="In meeting" />

    <TextView
        android:id="@+id/textView8"
        style="@style/text_blue_contains"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:onClick="onClick"
        android:clickable="true"
        android:drawableBottom="@drawable/line_white"
        android:tag="TMS me later"
        android:text="TMS me later" />

    <TextView
        android:id="@+id/textView9"
        style="@style/text_blue_contains"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:onClick="onClick"
        android:clickable="true"
        android:drawableBottom="@drawable/line_white"
        android:tag="At the toilet"
        android:text="At the toilet" />

    <EditText
        android:id="@+id/textCustomize"
        style="@style/text_blue_contains"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:tag="Customize"
        android:text="Customize" />

</LinearLayout>

【讨论】:

    【解决方案5】:

    对于 GUI 使用 HoloEverywhere。 https://github.com/Prototik/HoloEverywhere 如果您想在较旧的 Android 4.0 上使用 Holo 主题,HoloEverywhere 是最好的选择。

    对于下拉菜单,请在布局中使用 android:spinnerMode="dropdown",如 Stephen Kidson 所述。

    【讨论】:

      【解决方案6】:

      它是您应该使用的 AutocompleteTextView 小部件,但您已尝试使用 Spinner 小部件。

      如果我的猜测是正确的,请参考链接。 AutocompleteTextView

      干杯。

      【讨论】:

      • 是否有任何选项可以让 AutocompleteTextView 适合 Spinner?
      • 请参考上面的链接告诉我,你在寻找这样的小部件吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-16
      • 2015-05-26
      • 1970-01-01
      • 1970-01-01
      • 2016-06-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多