【问题标题】:Can't get rid of the ugly border around ImageButton无法摆脱 ImageButton 周围的丑陋边框
【发布时间】:2017-10-17 21:11:01
【问题描述】:

我正在开发一个 Android 应用程序,它有几个(下一步、返回、...等)Buttons,它们都是圆润透明的,我使用ImageButton 来处理那些Buttons,但问题是Button 周围总是有一个白色边框(我使用黑色 background,所以它非常难看),并且按钮从不显示为圆形,而是显示为某种方形。

这是我迄今为止尝试过的:

  1. 设置activity_mypage.xml文件中ImageButton的背景为

    android:background="@android:color/transparent"
    

    android:background="?android:selectableItemBackground"
    

    android:background="?android:selectableItemBackgroundBorderless"
    

    甚至到

    android:background="@null"
    

    但在 .xml 方面似乎没有任何效果。

  2. 我在 MyPage.java 文件上尝试了以下操作:

    myBtn.setBackgroundResource(0);
    

    还有

    myBtn.setBackground(null);  
    

我所做的任何事情似乎都没有效果,结果仍然相同(尽管它从 Button 周围删除了灰色边框,但它们都没有使 Button 完全透明)

这是应用上述任何一项之前的按钮截图:

这是应用任何这些属性后相同Button 的屏幕截图(如果我将其仅应用于xml 或仅应用于java 文件或两者都没有关系,因为所有结果都是同样):

这是我在 xml 中的 Buttons 之一的 xml 代码:

<ImageButton
    android:id="@+id/my_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="26dp"
    android:layout_marginEnd="37dp"
    android:layout_marginRight="37dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:srcCompat="@mipmap/my_btn"
    tools:layout_constraintBottom_creator="1"
    tools:layout_constraintRight_creator="1"
    android:background="@android:color/transparent"
    />

这是 .java 文件中的代码:

       ImageButton myBtn= (ImageButton) findViewById(R.id.my_btn);
       myBtn.setBackground(null);
       myBtn.setBackgroundResource(0);

我真的迷失了,我为此查询找到的每个结果都建议了上述方法之一,但这些方法似乎都不适合我..

【问题讨论】:

  • 您的图片周围似乎有白色边框。
  • 你能把你的图片(my_btn)也发上来吗
  • @VivekMishra 我之前尝试了许多透明图像,但它们都有相同的问题,Here 是我使用的同一图像的链接。
  • 你是否尝试设置 android:src 而不是 app:srcCompat
  • 也可能与问题有关:您不应将任何资源放在mipmap 文件夹中,它们仅用于启动器图标。将其移至相应的drawable 文件夹。

标签: java android xml


【解决方案1】:

只是改变

android:background="@android:color/transparent"

android:background="@null"

【讨论】:

  • 我已经按照问题中的说明尝试了这两种方法,但由于某种原因它不起作用..
【解决方案2】:

试试这个

android:src="@mipmap/yourimage"

而不是

app:srcCompat="@mipmap/my_btn"

【讨论】:

  • @LawaFazil 你想要什么样的动画?
【解决方案3】:

将此添加到您的按钮

android:background="@drawable/template"

并在您的可绘制文件夹中创建一个文件名模板 并将此代码添加到其中

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@drawable/btn2imagePressed"></item>
<item android:drawable="@drawable/btn1imageNotPressed"></item> </selector>

【讨论】:

  • 我找到了导致问题的原因,我现在将发布解决方案..
【解决方案4】:

你可以用ImageView代替ImageButton,这样就不会出现后台问题

<ImageView
            android:id="@+id/my_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="26dp"
            android:layout_marginEnd="37dp"
            android:layout_marginRight="37dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            android:src="@mipmap/my_btn"
        />

【讨论】:

  • 我希望按钮在单击时具有单击“动画”,这就是我使用 ImageButton 的原因
  • @LawaFazil 告诉我你是否使用 Activity 或 AppCompactActivity 扩展了你的活动类
【解决方案5】:

因此,在阅读了一些已发布的答案并尝试了我的代码中的建议后,我注意到 icon 被错误地加载到我的 mipmap 目录而不是 drawables 中,这是因为当我导入 @ 987654324@ 我选择了New -&gt; Image Asset 而不是Vector Asset 这导致我的“图标”失去了透明度......

感谢大家的回复和回答。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-21
    • 1970-01-01
    • 2018-09-19
    • 1970-01-01
    • 2012-03-08
    • 1970-01-01
    • 2011-03-15
    • 1970-01-01
    相关资源
    最近更新 更多