Android 使用材料设计的自定义搜索栏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24476072/
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 08:15:58  来源:igfitidea点击:

Custom seekbar with material design

androidmaterial-design

提问by greywolf82

I've got a seekbar in a layout for a custom dialog preference. I changed my styles.xml to use the new material desing. It works because it change text and checkboxes of my settings but I can't apply the color to my seekbar. It works only if I put a seekbar in an activity, it means I have to do something in my custom layout but I don't know what. I post styles.xml and the layout with the seekbar:

我在布局中有一个用于自定义对话框首选项的搜索栏。我更改了我的styles.xml 以使用新的材料设计。它有效,因为它更改了我的设置的文本和复选框,但我无法将颜色应用于我的搜索栏。只有当我在活动中放置一个搜索栏时它才有效,这意味着我必须在我的自定义布局中做一些事情,但我不知道是什么。我使用seekbar发布styles.xml和布局:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="AppBaseTheme" parent="android:Theme.Material">

        <!-- Main theme colors -->
        <!-- your app's branding color (for the app bar) -->
        <item name="android:colorPrimary">#FFFF4444</item>
        <!-- darker variant of colorPrimary (for status bar, contextual app bars) -->
        <item name="android:colorPrimaryDark">#FFCC0000</item>
        <!-- theme UI controls like checkboxes and text fields -->
        <item name="android:colorAccent">#FFFF00</item>
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
    </style>

</resources>

custom layout for dialog preference:

对话框首选项的自定义布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <SeekBar
        android:id="@+id/seek_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="6dip"
        android:theme="@android:style/Theme.Material"
        android:layout_marginTop="6dip" />

</LinearLayout>

回答by LOG_TAG

With Lollipop it's no more needed at all. The colors are applied well even in custom preference dialogs~greywolf82

有了 Lollipop,它就不再需要了。即使在自定义首选项对话框中,颜色也能很好地应用~greywolf82

I want to share few custom Libs

我想分享几个自定义库

enter image description here

在此处输入图片说明

You can use this Material Design lib,this also includes many other widgets.

您可以使用这个Material Design 库,这也包括许多其他小部件。

or

或者

enter image description here

在此处输入图片说明

Custom seek bar Lib DiscreteSeekBar

自定义搜索栏 Lib DiscreteSeekBar

回答by alanv

You don't need to specify the android:theme attribute on your SeekBar element. Instead, you should set up the default dialog and alert dialog themes in your AppBaseTheme:

您不需要在 SeekBar 元素上指定 android:theme 属性。相反,您应该在 AppBaseTheme 中设置默认对话框和警报对话框主题:

<style name="AppBaseTheme" parent="android:Theme.Material">
    ...
    <item name="android:dialogTheme">@style/AppDialogTheme</item>
    <item name="android:alertDialogTheme">@style/AppAlertTheme</item>
</style>

<style name="AppDialogTheme" parent="android:Theme.Material.Dialog">
    <item name="android:colorPrimary">#FFFF4444</item>
    <item name="android:colorPrimaryDark">#FFCC0000</item>
    <item name="android:colorAccent">#FFFF00</item>
</style>

<!-- This should extend Theme.Material.Dialog.Alert, but that style was
     incorrectly hidden in L-preview. It will be fixed in a future release.
     You can approximate the style using the last two MinWidth attributes.-->
<style name="AppAlertTheme" parent="android:Theme.Material.Dialog">
    <item name="android:colorPrimary">#FFFF4444</item>
    <item name="android:colorPrimaryDark">#FFCC0000</item>
    <item name="android:colorAccent">#FFFF00</item>
    <item name="android:windowMinWidthMajor">65%</item>
    <item name="android:windowMinWidthMinor">95%</item>
</style>