在棒棒糖设备上使用强调色的EditText

时间:2015-10-08 08:02:12

标签: android android-edittext

我是软件工程专业的学生,​​几个月前在业余时间开始学习android开发。

目前我正在制作我的第一个应用程序,同时在这个过程中学习并遇到了问题。

我正在使用DialogFragment,由于某种原因,我在主题中使用的Accent颜色仅在棒棒糖前设备(模拟器和物理设备)中被覆盖。

您可以注意到只有浮动提示在棒棒糖中着色。

For Example:

我的DialogFragment布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialog_add_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.design.widget.TextInputLayout
    android:id="@+id/input_title_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/input_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:hint="@string/dialog_add_title_hint"
        android:imeOptions="actionNext"
        android:inputType="text"
        android:singleLine="true" />

</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
    android:id="@+id/input_password_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/input_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:hint="@string/dialog_add_password_hint"
        android:imeOptions="actionGo"
        android:inputType="text"
        android:singleLine="true" />

</android.support.design.widget.TextInputLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
        android:id="@+id/input_cancel"
        style="?attr/buttonBarButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:layout_weight="1"
        android:text="Cancel" />

    <Button
        android:id="@+id/input_confirm"
        style="?attr/buttonBarButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:layout_weight="1"
        android:text="Add" />

</LinearLayout>

my values / styles.xml:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="colorControlHighlight">@color/colorHighlight</item>

    <!-- Context Action Mode will Overlay Toolbar -->
    <item name="windowActionModeOverlay">true</item>

</style>

我已经尝试了几种无法解决的解决方案:

  1. 覆盖colorControlActivated和colorControlNormal - 仅更改浮动提示色调
  2. 以编程方式更改色调,但仅在未聚焦时更改下划线:

    Drawable background = InputTextLayout.getEditText()。getBackground(); DrawableCompat.setTint(background,yourColor); InputTextLayout.getEditText()的setBackground(背景);

  3. 单独使用EditText,不包含在TextInputLayout中,也无济于事。

    任何想法?

    先谢谢

1 个答案:

答案 0 :(得分:1)

经过互联网彻底搜索后,我在stackoverflow中偶然发现了一个关于设置DialogFragment动画的问题 - here

该链接中的答案提到了Google工程师发布的帖子:

  

... DialogFragment围绕Dialog 只是一个包装器来帮助管理它的生命周期。对话框是顶级窗口,因此它们的动画是窗口动画,就像在所有其他情况下使用Dialog一样。因此,您可以通过在其WindowManager.LayoutParams中设置的主题或显式窗口动画样式来控制对话框的动画。

所以我决定检查如何设置DialogFragment的主题 &安培; CodePath发现了这个指南 - here

基本上,在你的App主题中,为了覆盖Dialog主题,你必须添加以下内容:

<style name="AppTheme" parent...>
    ....

    <!-- this will override DialogFragment theme -->
    <item name="android:dialogTheme">@style/MyDialogFragmentTheme</item>
    <!-- this will override AlertDialog theme -->
    <item name="android:alertDialogTheme">@style/MyAlertDialogTheme</item>
</style>

<style name="CustomAlertDialogTheme.Animation">
    ...
</style>

<style name="MyDialogFragmentTheme" parent="Theme.AppCompat.Light.Dialog">
    ...
</style>

在每个主题中,您可以覆盖colorPrimary,colorAccent等属性。

如果使用此方法使您的DialogFragment显示没有标题(它发生在我身上),则将以下内容添加到其样式中:

<item name="android:windowNoTitle">false</item>

奖金 - 添加动画

要将动画添加到AlertDialog或DialogFragment,请在其样式中编写以下内容:

<style name="MyDialogFragmentTheme" parent="Theme.AppCompat.Light.Dialog">
        ...
        <item name="android:windowAnimationStyle">@style/MyDialogFragmentTheme.Animation</item>

</style>

然后你需要为动画创建一个样式,例如:

<style name="MyDialogFragmentTheme.Animation">
        <item name="android:windowEnterAnimation">@anim/dialog_slide_in_up</item>
        <item name="android:windowExitAnimation">@anim/dialog_slide_out_down</item>
        <item name="android:interpolator">@android:interpolator/anticipate</item>
</style>

上面链接的CodePath指南中有更多信息。

请注意,这一切都发生在values / styles.xml

希望这有助于其他人。