1

我想要一个图像,在图像上我想放置一个较短的半透明视图并在该视图内放置一个文本视图,最后我想要所有这些可点击的显示基本点击效果的按钮。

我无法使用

<Button>
   <ImageView>
      <TextView/>
   </ImageView>
</Button>

我可以使用 Clickable=true 创建一个 LinearLayout。但是如何模仿按钮上存在的视觉按下效果呢?

更新:我添加了一张图片,显示了我希望如何看到它。认为这类似于 ImageButton(它应该具有按下效果)

在此处输入图像描述

UPDATE-2:我添加了另一个显示按下效果的图像。(类似于 Play 商店应用程序)。 在此处输入图像描述

4

3 回答 3

1

使用您选择的布局来创建您想要的效果并@android:style/Widget.Button在布局上设置样式。

此外,您可能需要调用setWillNotDraw(false)布局。

于 2013-02-26T15:28:31.200 回答
1

但是我怎样才能模仿按钮上存在的视觉按下效果呢?

如果我没记错的话,你可以在 LinearLayout 中添加 android:background="@drawable/some_click"

some_click.xml
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true" 
android:drawable="@drawable/pressed" /> <!-- pressed -->
<item
android:state_focused="true" 
android:drawable="@drawable/focused" /> <!-- focused -->
<item
android:drawable="@drawable/default" /> <!-- default -->
</selector>
于 2013-02-26T15:29:45.017 回答
1

创建一个包含图像和文本等的 ViewGroup(例如 LinearLayout/RelativeLayout/etc)。

使这个 ViewGroup 可点击(例如,通过为其分配一个 OnClicListener)。

为该 ViewGroup 分配一个背景可绘制对象。

确保此背景可绘制对象是状态列表可绘制对象: https ://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html http://developer.android.com/guide/topics/resources/ drawable-resource.html#StateList

将适当的可绘制对象分配给各种状态(最重要的是 state_pressed,但您可能还想处理其他状态)。

当用户按下按钮时,将显示适当的可绘制对象,并且看起来好像 ViewGroup 是一个按钮(可以按下的东西)。

OP 显示按下状态的新图像后更新:

添加一个位于图像/文本/等之上的视图,其背景具有 StateListDrawable:

<RelativeLayout >
    <ImageView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/somerealpngfile"
        ... 
    />
    <TextView  
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        ... 
    />
    <View 
        android:id+"@+id/clickable_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/selectable_background"
        android:clickable="true"

        ... 
    />
</RelativeLayout>

res/drawable/selectable_background.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/grid_state_pressed"/>
    <item android:state_focused="true" android:drawable="@color/grid_state_focused"/>
    <item android:drawable="@android:color/transparent"/>
</selector>

res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
  ...
  <color name="grid_state_pressed">#BB7dbcd3</color>
  <color name="grid_state_focused">#777dbcd3</color>
  ...
</resources>

其中颜色grid_state_pressedgrid_state_focused是半透明的(即它们的 alpha 小于 255)。

当用户单击您的“按钮”时,ViewR.id.clickable_view将处理 onClick 并更改其背景颜色,从而导致图像和文本以半透明的方式闪耀。

于 2013-02-26T15:32:58.413 回答