【问题标题】:ARGB not working properly in Android appARGB 在 Android 应用程序中无法正常工作
【发布时间】:2011-09-26 05:48:48
【问题描述】:

当在我的 ListView 中按下它时,我试图将透明背景应用于我的行。我像这样创建文件 list_selector.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/argb80804040" />
</selector> 

在我的 news_item.xml 中,我将背景定义如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/list_selector">

<ImageView
   android:id = "@+id/img"
   android:layout_width="95dip"
   android:layout_height="75dip"
   android:layout_alignParentLeft = "true"
   android:paddingTop="10dip"
   android:layout_marginLeft="10dip"
   android:paddingBottom="10dip"
   android:src="@drawable/no_news_image_small" />     

<TextView android:id="@+id/title"
    android:layout_gravity="center_vertical"
    android:layout_width="fill_parent"
    android:layout_weight="1.0"
    android:layout_height="wrap_content" 
    android:textSize="12sp" 
    android:textStyle="bold"
    android:paddingTop="5dip"
    android:layout_marginLeft="110dip"
    android:textColor="#21B6EF"
    android:singleLine="true" />

<TextView android:layout_gravity="center_vertical"
    android:layout_width="fill_parent"
    android:layout_weight="1.0"
    android:layout_height="wrap_content" 
    android:layout_marginLeft="110dip" 
    android:textSize="10sp" 
    android:id="@+id/description" 
    android:layout_below="@id/title"
    android:paddingBottom="10dip" 
    android:layout_marginBottom="5dip" 
    android:textColor="#fff"/>

</RelativeLayout>

但是,它显示为纯色而不是 ARGB 颜色。我的drawable定义如下:

<resources>
    <item type="drawable" name="transparent_orange">#64ff7b00</item>
    <item type="drawable" name="argb80804040">#80804040</item>
    <item type="drawable" name="argb40000000">#40000000</item>
    <item type="drawable" name="argb80408040">#80408040</item>
</resources>

我是否需要做其他事情才能使 aplha 颜色正常工作?我正在我自己的设备上尝试(motorola defy),并且我还在使用另一个使用相同 alpha 颜色的应用程序,并且该颜色在此应用程序中看起来不错,但在我的设备中却没有。

非常感谢您的任何回复 T

【问题讨论】:

    标签: android


    【解决方案1】:

    我不太明白你想要达到什么目的,但我的建议会是

    在 res/values/colors.xml 中定义你的颜色

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
      <color name="transparent_orange">#64ff7b00</color>
      ...
    </resources>
    

    然后你可以在 res/drawable/test.xml 中定义一个drawable用作背景:

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

    最后,您可以使用生成的drawable作为目标容器的属性

    android:background="@drawable/blue_linear_gradient"
    

    【讨论】:

    • @Renaud 谢谢。我在这里测试了你的例子,它可以工作,但颜色不透明。它似乎我的 alpha 值不起作用。我得到一个没有任何透明度的纯橙色。我这里有很多示例 ARGB 颜色代码,但它们中的任何一个都可以用作 ARGB 颜色,因为没有应用透明度。
    • @Thiago:在形状定义中使用 (应用于带有 android:background="@drawable/the_shape" 的 TextView 和带有android:background="#ffffff") 在这里工作。您能否提供一个我们可以研究的最小示例?
    • 我正在使用带有 android:background="@drawable/list_selector" 的 RelatyveLayout,并且在 list_selector 内我有 。 RelativeLayout 是我的列表项的父布局元素,因此我将透明背景应用于它。仍然没有透明度,只有纯橙色。谢谢@Renaud
    • 好吧,我将不得不放弃...我尝试了两个示例,但什么也没尝试...只有纯色,没有透明度。我的设备上运行了成功应用透明度的应用程序,但我的应用程序没有这样做。谢谢
    猜你喜欢
    • 2016-03-19
    • 2015-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多