android - 将 ImageView 放在 Button android 上

标签 android android-layout

我正在尝试使用 RelativeLayout 将 ImageView 放置在 Button 上。这是我的xml:

<RelativeLayout
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:layout_weight="0.50" >

                <Button
                    android:id="@+id/btnFindDaysInBetween"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@color/blue_500"
                    android:text="@string/dt_text_days" />

                <ImageView
                    android:id="@+id/imageview_find_days_in_between"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:contentDescription="@string/empty"
                    android:src="@drawable/ic_check_circle_white" />
            </RelativeLayout>

这是图片截图:

enter image description here

如您所见,ImageView 的 src 图像是不可见的。但是,如果我将背面的按钮更改为 ImageView,则顶部 ImageView 的图像是可见的。请引用下面..

更改的 xml:

 <RelativeLayout
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:layout_weight="0.50" >

                <!-- 
                <Button
                    android:id="@+id/btnFindDaysInBetween"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@color/blue_500"
                    android:text="@string/dt_text_days" />
                -->
                <ImageView
                    android:id="@+id/imageview_find_days"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_centerInParent="true"
                    android:contentDescription="@string/empty"
                    android:src="@drawable/ic_send_black" />

                <ImageView
                    android:id="@+id/imageview_find_days_in_between"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:contentDescription="@string/empty"
                    android:src="@drawable/ic_check_circle_white" />
            </RelativeLayout>

修改了xml的截图:

enter image description here

我在第一个布局中做错了什么?

最佳答案

原因其实非常很简单。 :) 我们如此沉迷于 2D 的思考,以至于我们忽略了 elevation - 在 Z 中。

您的第一个布局没有任何问题。 Button 只是比 ImageView 具有更高的 elevation - 正好 1dp 更高。因此,无论你如何排列这两个 View ,Button 都会升到上面。

一点证据:

一个Button,默认获取Widget.Material.Button样式:

<!-- Bordered ink button -->
<style name="Widget.Material.Button">
    <item name="background">@drawable/btn_default_material</item>
    <item name="textAppearance">?attr/textAppearanceButton</item>
    <item name="minHeight">48dip</item>
    <item name="minWidth">88dip</item>
    <item name="stateListAnimator">@anim/button_state_list_anim_material</item>
    <item name="focusable">true</item>
    <item name="clickable">true</item>
    <item name="gravity">center_vertical|center_horizontal</item>
</style>

引入这个海拔的属性是android:stateListAnimatorStateListAnimator类似于StateListDrawable,提供状态变化动画。完整的 xml 在这里:Link .但这里是按钮的基本状态:

<!-- base state -->
<item android:state_enabled="true">
    <set>
        <objectAnimator android:propertyName="translationZ"
                        android:duration="@integer/button_pressed_animation_duration"
                        android:valueTo="0"
                        android:startDelay="@integer/button_pressed_animation_delay"
                        android:valueType="floatType"/>
        <objectAnimator android:propertyName="elevation"
                        android:duration="0"
                        android:valueTo="@dimen/button_elevation_material"
                        android:valueType="floatType" />
    </set>
</item>

如您所见,按钮的高程值设置为@dimen/button_elevation_material:

<dimen name="button_elevation_material">1dp</dimen>

这就是 ImageView 最终位于 Button 后面/之下的方式。

那么,我们能做些什么呢?

直接的解决方案是将 ImageView 的 高度设置为相同的量 - 1dp

另一个需要做一些工作的解决方案是删除 Button 的 高度而不是更改 ImageView 的。基于默认的 StateListAnimator,我们可以创建自己的 - 并移除海拔。然后,在您的 res/values-v21/styles.xml 中,定义一个继承自 Widget.Material.Button 的样式:

<style name="MyDepressedButtonStyle" parent="android:Widget.Material.Button">
    <item name="android:stateListAnimator">@anim/customized_state_animator</item>
</style>

现在,在你的 Button 上设置这个样式:

<Button
    style="@style/MyDepressedButtonStyle"
    ....
    .... />

编辑:

其实我们可以直接应用自定义的StateListAnimator:

<Button
    android:stateListAnimator="@anim/customized_state_animator"
    ....
    .... />

不用走景区路线!

关于android - 将 ImageView 放在 Button android 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28560692/

相关文章:

android - 如何修复在 Nexus 设备中出现移动的导航栏?

Java - 使用递归返回列表

java - 如何在回收器 View 适配器中获取上下文

android - 按下后退按钮时显示 toast 但 ondestroy() 不工作

Android 对话框编号选择器

android - 如何以编程方式使小按钮包裹小文本

android - 如何在 Google Cloud 上安装 Wowza Streaming Engine?

android - 尝试了解 Android 布局、图像大小和关系

android - slider 选择 -24 和 24 之间的数字,步长为 0.5

android - 如何在 Android ViewPager 中更改当前选项卡荧光笔颜色?