首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >android如何在不产生灰色背景的情况下将视图设置在按钮顶部

android如何在不产生灰色背景的情况下将视图设置在按钮顶部
EN

Stack Overflow用户
提问于 2019-06-14 08:22:35
回答 3查看 586关注 0票数 0

我正在尝试实现一个布局,其中我有一个按钮上的ImageView和TextView。

目标:

我试着使用"android:elevation“或"android:translationZ”来实现这一点,但结果真的很令人失望,视图可以在顶部,但它也会有一个灰色的背景,我不知道如何摆脱它。

我得到的是:

尝试使用"android:background="@android:color/transparent"",,但不起作用。

我的布局:

代码语言:javascript
复制
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="@dimen/table_page_turn_item_height">

<Button
    android:id="@+id/turn_btn_voice"
    android:layout_width="@dimen/table_page_turn_btn_width"
    android:layout_height="@dimen/table_page_turn_btn_height"
    app:layout_constraintTop_toBottomOf="@id/turn_text_name"
    app:layout_constraintRight_toRightOf="parent"
    android:gravity="center"       
    android:background="@drawable/rounded_corner_generic_light_blue_fill" 
    />

<ImageView
    android:id="@+id/turn_play_icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintLeft_toLeftOf="@id/turn_btn_voice"
    app:layout_constraintTop_toTopOf="@id/turn_btn_voice"
    app:layout_constraintBottom_toBottomOf="@id/turn_btn_voice"
    android:translationZ="2dp"
    android:src="@drawable/ic_play_arrow_24px"
    android:background="@android:color/transparent"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toTopOf="@id/turn_btn_voice"
    app:layout_constraintBottom_toBottomOf="@id/turn_btn_voice"
    app:layout_constraintLeft_toRightOf="@id/turn_play_icon"
    android:layout_marginStart="20dp"
    android:elevation="2dp"
    android:textColor="@color/colorEugraBlue"
    android:text="1:30"
    android:textSize="@dimen/font_size_ctrl_large"/>

</android.support.constraint.ConstraintLayout>

谢谢!

编辑:已尝试将标高设置为1,但ImageView仍将隐藏在按钮后面,只留下灰色边框。

左边的灰色方块是标高为1的视图

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-06-14 12:17:39

我猜这实际上不是一个问题,Android Studio预览面板中的东西让人困惑,任何高度高于1的视图都将被赋予灰色背景,但在模拟器上它实际上是可以的。

对于这个特定的情况,我将elevation设置为5,得到了所需的布局。低于或等于2的高程将隐藏按钮后面的视图。

所以我假设按钮的默认高度是2。

票数 0
EN

Stack Overflow用户

发布于 2019-06-14 08:31:00

Button的高程高于其他两个视图。对于ImageView来说,它就是更高的1dp。因此,只需将ImageView的高程设置为相同的高程- 1dp如下:

代码语言:javascript
复制
<ImageView
    android:id="@+id/turn_play_icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintLeft_toLeftOf="@id/turn_btn_voice"
    app:layout_constraintTop_toTopOf="@id/turn_btn_voice"
    app:layout_constraintBottom_toBottomOf="@id/turn_btn_voice"

    android:elevation="1dp"

    android:src="@drawable/ic_play_arrow_24px"
    android:background="@android:color/transparent"/>

我编写的模拟应用程序UI看起来与标高很好。IDE或仿真器的预览窗格可能会显示与实际应用程序UI不同的内容:

票数 1
EN

Stack Overflow用户

发布于 2019-06-14 09:30:00

不要使用标高,请使用此

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="@color/colorBlack">
    </LinearLayout>

     <!--It will be on top of linear layout-->
    <TextView
        android:textColor="@color/white"
        android:text="test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</androidx.constraintlayout.widget.ConstraintLayout>

结论: Android的布局顺序是落后的。如果想要将小部件A放在小部件B的顶部,只需在xml上将小部件A添加到小部件B的下面

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56590010

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档