【问题标题】:Android How to change TimePicker theme?Android 如何更改 TimePicker 主题?
【发布时间】:2013-04-14 09:45:23
【问题描述】:

目前在我的应用程序中使用 Theme.Sherlock 主题。是否可以更改 TimePicker 小部件的主题。我想将蓝色分隔线更改为绿色,将白色文本更改为黑色。

我还没有找到任何可用的东西(不幸的是,android-holo-colors.com 工具中缺少 TimePicker),希望你有一个好主意。

谢谢!

【问题讨论】:

  • 风滚草帖子还是什么?我也想知道这是否可能。

标签: android themes customization android-theme timepicker


【解决方案1】:

由于 Android SDK 不允许您在 timepicker 中修改元素。你需要一种方法来进行黑客攻击。 对于文本颜色,您需要更改主题。这是我的片段中的一个小例子。 重点是“R.style.Picker_Black”,我将主题设置为“Theme.Holo.Light”,以便在时间选择器中显示“黑色”文本颜色。

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.Picker_Black);
    LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
    View v = localInflater.inflate(R.layout.fragment_booking, container, false);
    return v;
  }

在“style.xml”中定义样式

  <style name="Picker.Black" parent="android:Theme.Holo.Light">
    <item name="android:editTextStyle">@style/Widget.EditText.White</item>
  </style>

对于分频器:

public class CustomTimePicker extends TimePicker {
  public CustomTimePicker(Context context) {
    super(context);
    init();
  }

  public CustomTimePicker(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
  }

  public CustomTimePicker(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
  }

  private void init() {
    removeAllDividers(this);
  }

  private void removeAllDividers(ViewGroup picker) {
    for (int i = 0; i < picker.getChildCount(); i++) {
      View view = picker.getChildAt(i);
      if (view instanceof NumberPicker) {
        try {
          Field field = NumberPicker.class.getDeclaredField("mSelectionDivider");
          field.setAccessible(true);
          field.set(view, null);
        } catch (Exception e) {
        }
      } else if (view instanceof ViewGroup) {
        removeAllDividers((ViewGroup) view);
      }
    }
  }
}

【讨论】:

    【解决方案2】:

    虽然我对这个答案有点晚了,但对于那些将来会想知道的人来说。加gradle

    compile 'com.github.rey5137:material:1.2.2'
    

    这是我见过的best library。用它。它包含您需要的所有信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-11
      • 2020-09-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多