【问题标题】:What's making the button background white, shouldn't it be transparent?是什么使按钮背景变白,它不应该是透明的吗?
【发布时间】:2019-01-21 00:03:45
【问题描述】:

正如您在此处看到的,按钮的背景是透明的。我似乎无法在任一 xml 文件中找到它设置为白色的位置。该按钮标识为btnCapture,代码如下。

activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextureView
        android:id="@+id/textureView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/btnCapture" />

    <Button
        android:text="@string/capture"
        android:id="@+id/btnCapture"
        android:layout_width="wrap_content"
        android:layout_height="92dp"
        android:background="@drawable/rounded_button"


        android:layout_alignParentBottom="true"
        android:layout_centerInParent="true"
        tools:layout_editor_absoluteX="157dp"
        tools:layout_editor_absoluteY="202dp" />

</RelativeLayout>

rounded_button.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <stroke android:color="#80000000"
    android:width="2dp"></stroke>
    <stroke android:color="#D3D3D3"></stroke>
    <size android:width="60dp"
        android:height="80dp"></size>
</shape>

【问题讨论】:

  • 问题与 Java 无关。使用您的 rounded_button.xml 文件的快照更新问题。这就是问题所在。
  • @Taslim 完成。但是仍然看不到该文件中的问题所在。
  • 我已经回答了你的问题。

标签: java android xml android-studio


【解决方案1】:

您不会更改 Java 代码中与按钮 UI 相关的任何内容,所以不,这不是因为 Java 代码。它与 XML 相关。

您的按钮背景是一个名为“rounded_button”的可绘制对象,但您尚未发布该可绘制文件的内容。所以我们能做的就是猜测rounded_button drawable是白色的?

【讨论】:

  • 添加了rounded_button.xml
  • rounded_button.xml 中有 2 个“stroke”元素,没有“solid”元素。而且 XML 中的颜色都不是透明的。通读和/或 google 'android shape xml'。
【解决方案2】:

您可以采取两个快速步骤来使这项工作完全按照您的期望进行:

首先,将视图从 Button 更改为 TextView 并应用 rounded-button.xml 背景。这应该立即起作用。尝试为按钮设置背景有点棘手,如 here 所示。

其次,我注意到您的rounded-button.xml 文件没有solid 元素。实体元素允许您设置布局的背景。出于这个原因,我将您的stroke 元素之一更改为solid 元素(无论如何都不应该有两个stroke 元素)。您更新后的rounded_button.xml 代码如下所示:

PS:不要忘记将您的 activity_main.xml 中的视图从 Button 更改为 TextView!

<shape 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <stroke android:color="#80000000"
        android:width="2dp">
    </stroke>

    <solid 
        android:color="#D3D3D3">
    </solid>

    <size 
        android:width="60dp"
        android:height="80dp">
    </size>

</shape>

我希望这会有所帮助。编码愉快!

【讨论】:

  • 嗨!谢谢你的尝试,我申请了。我将其更改为 TextView 并将 rounded_button.xml 文件更改为您放置的文件,但仍输出白框,文本现在位于形状之外,并且形状奇怪地扭曲了。正如你在这里看到的:prnt.sc/maqqpq
  • android:gravity="center" 添加到您的TextView xml 以固定文本的位置。要消除白色,请将 RelativeLayout 的背景更改为白色。
  • 如果我的上述建议有效,请将其标记为正确答案。 ;-)
  • 所以,回复明确地修复了该区域之外的文本。那工作得很好。尽管我在 RelativeLayout 中添加了白色背景,但按钮周围的白色区域仍然存在。
  • 对不起,我的意思是你应该将相对布局的背景从白色更改为任何其他颜色。
猜你喜欢
  • 1970-01-01
  • 2011-05-24
  • 2012-02-27
  • 1970-01-01
  • 1970-01-01
  • 2014-05-07
  • 1970-01-01
  • 1970-01-01
  • 2011-12-08
相关资源
最近更新 更多