【问题标题】:How to make a shape have solid color that is 50% alpha of an already defined color?如何使形状具有已定义颜色的 50% alpha 的纯色?
【发布时间】:2017-12-03 16:26:12
【问题描述】:

我在一个可绘制资源文件中定义了一个形状,用作我的 TextView (scrim_background.xml) 的背景:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid
        android:color="@color/colorAccent"/>
</shape>

我的 colors.xml 是:

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


    <color name="white">#fff</color>
    <color name="light_text">#dfff</color>

    <color name="colorPrimary">@color/primary</color>
    <color name="colorPrimaryDark">@color/primary_dark</color>

    <color name="colorAccent">@color/accent</color>


    <color name="primary">#F44336</color>
    <color name="primary_dark">#D32F2F</color>
    <color name="primary_light">#FFCDD2</color>
    <color name="accent">#607D8B</color>

    <color name="accentAlpha">#DD607D8B</color>
    <color name="primary_text">#212121</color>
    <color name="secondary_text">#757575</color>
    <color name="icons">#FFFFFF</color>
    <color name="divider">#BDBDBD</color>

</resources>

我的文本视图的样式是:

<style name="BeerItemTextStyleTitle" parent="@android:style/TextAppearance.Medium">
        <item name="android:textColor">@color/light_text</item>
        <item name="android:textStyle">bold</item>
        <item name="android:background">@drawable/scrim_background</item>
        <item name="android:gravity">start|center_vertical</item>
        <item name="android:paddingStart">@dimen/space_small</item>
        <item name="android:paddingEnd">@dimen/space_small</item>
    </style>

我希望背景颜色具有 colorAccent 的 50% 的透明度。

类似:

<color name="accent">#88607D8B</color>

但我不想修复这些值,因为我一直在更改配色方案。

我尝试将 alpha 直接添加到我的 TextView,例如:

<item name="android:alpha">50</item>

但这并不合适,因为它也为文本颜色添加了透明度。我希望只有背景颜色是透明的。

我不想以编程方式执行此操作,因为这样我必须找到我所有的文本视图并一一更改。

-- 编辑--

我尝试创建一个名为 bitmap_scrim_background 的元资源文件:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <bitmap
            android:src="@drawable/scrim_background">
        </bitmap>
    </item>
</layer-list>


<style name="BeerItemTextStyleTitle" parent="@android:style/TextAppearance.Medium">
    <item name="android:textColor">@color/light_text</item>
    <item name="android:textStyle">bold</item>
    <item name="android:background">@drawable/bitmap_scrim_background</item>
    <item name="android:gravity">start|center_vertical</item>
    <item name="android:paddingStart">@dimen/space_small</item>
    <item name="android:paddingEnd">@dimen/space_small</item>
</style>

但我认为在 Android 中情况并非如此......它会抛出:

android.view.InflateException: Binary XML file line #36: Binary XML file line #36: Error inflating class TextView
                                                                                       Caused by: android.view.InflateException: Binary XML file line #36: Error inflating class TextView

Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #5: <bitmap> requires a valid 'src' attribute

【问题讨论】:

    标签: android android-view android-resources android-styles android-color


    【解决方案1】:

    我猜稀松布背景是一个可绘制的 xml 文件。你可以在你的drawable文件中使用accentColor。

    例如

    <shape
        xmlns:android="http://schemas.android.com/apk/res/android">
        <gradient
        android:angle="90"
        android:centerY="0.3"
        android:startColor="@color/accent"
        android:endColor="#00000000"
        android:type="linear"/>
    </shape>
    

    【讨论】:

    • 在您的示例中,它创建了一个渐变。
    • 了解 scrim_background 到底是什么会很有帮助。
    • 这是一个可绘制的资源文件。我将编辑问题。
    • 我四处挖掘。在纯 xml 中似乎没有简单的方法。你提醒了我为什么我开始讨厌 android。
    【解决方案2】:

    你看过ColorUtils.setAlphaComponent()吗?

    int setAlphaComponent (int color, int alpha)
    

    您可以使用以下命令查询任何颜色:

    int x = getColor(R.color.yourColor)
    

    然后使用以下命令创建该颜色的任何 alpha 版本:

    int newX = ColorUtils.setAlphaComponent(x, newAlpha);
    

    【讨论】:

      【解决方案3】:

      使用它来创建滚动条拇指 50% 透明与现有的非透明颜色

      <?xml version="1.0" encoding="utf-8"?>
      <shape xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="rectangle"
          android:tint="@color/color_white_alpha_50"   //#80FFFFFF
          android:tintMode="multiply">
          <solid android:color="?colorControlNormal" />
          <corners android:radius="2dp" />
      </shape>
      

      【讨论】:

        【解决方案4】:

        您可以使用以下方式以编程方式执行此操作:

        textView.getBackground().setAlpha(50);
        

        【讨论】:

        • 我认为它可以工作,但我想将它添加到样式中,否则 e 将不得不找到所有 TextViews 和此代码。
        • 一种解决方案可能是创建一个扩展 TextView 的自定义视图 AlphaTextView,您可以在其中更改背景 alpha。
        • 是的,这是一个解决方案。但我认为应该是一种通过资源来做到这一点的方法。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-28
        • 2017-10-28
        相关资源
        最近更新 更多