【问题标题】:Mask of the components of android when you click点击时android组件的掩码
【发布时间】:2026-02-22 13:45:01
【问题描述】:

我想在android中获得这种效果,我尝试设置布局的背景但没有成功,这就像在布局的元素上放置了一个蒙版。例如,当您单击 google play 上的按钮时,如下所示:

当您单击它时,它会在蓝色按钮上放置一个蒙版。

有人可以帮我吗?

谢谢。


我做了以下,我用图像和文本创建了布局。

<LinearLayout
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:gravity="center_horizontal"
   android:orientation="vertical">

  <ImageView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:src="@drawable/android" />

      <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="APP" />
</LinearLayout>

并创建框架布局

<FrameLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_default"
    android:clickable="true">

       <include
           android:layout_gravity="center_vertical"
          layout="@layout/test1" />

</FrameLayout> 

但只有背景被改变,图像和文字没有放置蒙版。我是不是做错了什么?

【问题讨论】:

标签: android layout


【解决方案1】:

实现这种效果我首先想到的是:

1. 创建一个布局(Linear/Relative),其中包含图标和文本(在您的示例中为 android 图标和 APPS 文本。)

2. 将两个维度都设置为wrap-content

3. 将此布局放入FrameLayout

4.FrameLayout 中添加一个按钮并将其设置为match-parent,为此按钮创建一个像这样的选择器并将其应用为背景:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

 <!-- pressed -->

 <item android:state_pressed="true"
       android:drawable="@drawable/botton_shape_pressed" /> 

 <!-- focused -->      

 <item android:state_focused="true"
       android:drawable="@drawable/botton_shape_selected" />

 <!-- default -->      

 <item android:drawable="@drawable/botton_shape_released" />

5.为压制的纹理创建一个半透明的蓝色纹理。对于其他两个状态,应用完全透明的纹理。这将导致掩码影响您寻找。

【讨论】: