如何设置不同的主题,为微调的下拉列表?

问题描述:

使用示例:

的微调是黑暗的主题,但我想下拉到轻的主题。

The Spinner is dark themed, but I want the dropdown to be light themed.

Android的中号

在新的Andr​​oid 6.0,微调目前拥有安卓popupTheme 参数,它允许您设置用于弹出(下拉)

New in Android 6.0, Spinner now has the android:popupTheme parameter which allows you to set the theme used for the popup (dropdown).

您可以使用它作为这样:

You can use it as so:

<Spinner
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:popupTheme="@android:style/ThemeOverlay.Material.Light" />

这将适用于运行API级别23+的设备,而不是在运行Android较低版本的设备。

That will work on devices running API level 23+, but not on devices running a lower version of Android.

AppCompat

这就是AppCompat进来。它的微调的实施还支持 popupTheme ,但它更复杂一点,以获得正确的。

This is where AppCompat comes in. Its Spinner implementation also supports popupTheme, but it's a bit more involved to get right.

<Spinner
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

在这之后,你需要更新你的适配器能够与AppCompat工作。你可以通过使其成为实施新的ThemedSpinnerAdapter$c$c>接口。

public class MyAdapter extends BaseAdapter implements ThemedSpinnerAdapter {

   Theme getDropDownViewTheme() { ... }

   void setDropDownViewTheme(Theme theme) { ... }

}

这些方法所使用的微调,能够判断哪些主题用于充气任何下拉意见适配器。为了使这个尽可能容易,我们已经给了你一个Helper$c$c>类,你可以插入到您的适配器。

These methods are used by Spinner to be able to tell the Adapter which theme to use for inflating any drop down views. To make this as easy as possible we have given you a Helper class which you can plug in to your adapter.

这意味着,您的适配器变成是这样的:

This means that your adapter becomes something like:

public class MyAdapter extends BaseAdapter implements ThemedSpinnerAdapter {
  private final ThemedSpinnerAdapter.Helper mDropDownHelper;

  public MyAdapter(Context context) { 
    mDropDownHelper = new ThemedSpinnerAdapter.Helper(context);
  }

  @Override
  public View getDropDownView(int position, View convertView, ViewGroup parent) {
    View view;

    if (convertView == null) {
      // Inflate the drop down using the helper's LayoutInflater
      LayoutInflater inflater = mDropDownHelper.getDropDownViewInflater();
      view = inflater.inflate(R.layout.my_dropdown, parent, false);
    }

    // ...

    return view;
  }

  @Override
  public void setDropDownViewTheme(Theme theme) {
    mDropDownHelper.setDropDownViewTheme(theme);
  }

  @Override
  public Theme getDropDownViewTheme() {
    return mDropDownHelper.getDropDownViewTheme();
  }
}