0

我有一个 Android 应用程序,它使用我在右上角添加MapView的控件(移动到用户的当前位置)。ImageButton我遇到的问题是 ImageButton 控件的背景太透明了,但是更改它android:background="#BBFFFFFF"会改变背景的大小并删除您通常在按下按钮时看到的蓝色“闪光” - 我希望保留这两个品质。

我从这样的事情开始:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="my api key"
/>

<ImageButton android:id="@+id/googlemaps_select_location"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_marginTop="13dp"
    android:layout_marginRight="13dp"

    android:src="@drawable/device_access_location_found"/> 

</RelativeLayout>

这实现了如下所示:

ImageButton 控件的背景太透明

所以我补充说:

android:background="#BBFFFFFF"

我明白了:

ImageButton 控件具有更不透明的背景,但其他属性受到影响

请注意,虽然这基本上是我想要的不透明度级别,但更改背景会影响填充,并且按下时也不会显示蓝色“闪光”(这显然没有在这个问题中说明)。

所以我的问题是,如何在未按下状态下仅更改背景颜色/不透明度,同时保留按钮的其他视觉质量?我简要阅读了有关 Android 样式和主题的内容,但甚至无法弄清楚该按钮的样式/主题来自何处,以及如何在保留所有其他视觉功能的同时覆盖背景颜色/不透明度。

4

2 回答 2

1

问题

当您为视图背景分配固定颜色时,您将用您定义的固定颜色替换视图中的默认背景。

实际上,按钮的背景并不是简单的固定颜色。它是颜色或可绘制对象的状态列表,这意味着,根据按钮状态(焦点、选定、按下等),使用不同的背景,从而在按下按钮时产生“闪烁”动画。如果你用一个简单的固定颜色替换这个状态列表,而不是依赖于按钮状态,你会得到一个固定的背景(即按下按钮时不改变)。

解析度

有一个 xml 参数可用于更改图像视图的 alfa(即透明度),即:

        android:alpha="1"

其中1上面的值可以是 0 到 1 之间的任何浮点数,1 是最大不透明度。但是,我相信这并不能解决您的问题,因为如果我正确理解您的问题,您想更改背景的 alfa 而不是图像 alfa。无论如何,默认值似乎是1.

应该为您工作的一种可能性是定义一个selector用作背景。选择器将根据他的状态选择可绘制对象。

例子

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="true" android:drawable="@android:color/darker_gray" />
  <item android:drawable="@android:color/white" />
</selector>

将上面的 xml 文件drawable-xxxx以名称保存在您的文件夹中。my_selector 在此示例中,我使用标准的 android 颜色,但您可以定义自己的颜色。您需要为希望具有不同颜色的每个不同按钮状态分配一种颜色。

然后您需要将您的ImageView背景定义为您在上面定义的选择器:

<ImageButton
    android:id="@+id/googlemaps_select_location"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="13dp"
    android:layout_marginTop="13dp"
    android:background="@drawable/my_selector"
    android:src="@drawable/device_access_location_found" />

通过以上更改,按钮使用的免费颜色将在按下按钮时发生变化,并且您可以具有“闪光”效果。

于 2012-10-20T18:47:15.013 回答
0

为了我的目的,我最终使用了一种样式来继承外观,Widget.ImageButton只需进行一些小的调整:

我的/res/values/styles.xml文件现在看起来像:

<resources>

<style name="AppTheme" parent="android:Theme.Light" />

<style name="my_loc_btn_style" parent="@android:style/Widget.ImageButton">
    <item name="android:layout_marginTop">8dp</item>
    <item name="android:layout_marginRight">8dp</item>
</style>

</resources>

我的布局文件有:

<ImageButton android:id="@+id/googlemaps_select_location"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    style="@style/my_loc_btn_style"
    android:src="@drawable/device_access_location_found"/>

这似乎继承了一个Widget.ImageButton似乎只是稍微透明的背景,这就是我所追求的,所以我现在根本不设置透明度。

于 2012-10-23T04:58:52.187 回答