【问题标题】:Make borderless image button small on Android在 Android 上使无边框图像按钮变小
【发布时间】:2016-09-08 13:33:00
【问题描述】:

我的应用中有一些可关闭的视图,并且在标题中有一个关闭按钮。我想让这个按钮无边框且小。
代码:

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="?android:attr/borderlessButtonStyle"
    android:layout_margin="0dp"
    android:padding="0dp"
    android:src="@android:drawable/ic_menu_close_clear_cancel"/>

结果按钮是无边框的,但在十字图像周围有很多空白位置(在屏幕截图上点击按钮以使空白区域可见)

我该如何解决?

【问题讨论】:

  • 你会分享你的输出图像吗......

标签: android button borderless


【解决方案1】:

你应该使用:

  android:background="?selectableItemBackgroundBorderless"

【讨论】:

  • 无法检查您的解决方案,但我相信它可行并且看起来很漂亮。接受!
【解决方案2】:

添加

 android:minHeight="0dp"
 android:minWidth="0dp"
 android:padding="2dp"

结果

此外,我将使用负边距将按钮放置在靠近角落的位置。

【讨论】:

    【解决方案3】:

    您可以在 ImageButton 上使用以下内容来删除“边框”:

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

    android:background="@null"
    

    如果您想在用户单击按钮时更改背景,您可以创建一个选择器。示例ImageButton doesn't highlight on click with Transparent background

    【讨论】:

      【解决方案4】:

      随着ConstraintLayout的引入,您可以使用约束来减小无边框ImageButton的宽度,默认情况下太宽了,通过将其限制为与高度一样宽:

      <android.support.constraint.ConstraintLayout 
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:id="@+id/my_closeable_view"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
      
          <ImageButton
              style="?android:attr/borderlessButtonStyle"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:contentDescription="@string/action_close"
              android:src="@android:drawable/ic_menu_close_clear_cancel"
              app:layout_constraintDimensionRatio="w,1:1"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintTop_toTopOf="parent" />
      
      </android.support.constraint.ConstraintLayout>
      

      我不建议您将其设置为比此更小,因为触摸目标至少需要为 48dp x 48dp,对于 Google's Material Design guidelines,否则用户将很难尝试触摸它。

      如果您确实需要它看起来更靠近视图的边缘,您始终可以使用未居中的图像(一侧或两侧具有透明填充),但我会尽量避免这样做这种解决方法并尝试重新考虑我的应用程序的设计,以适应按钮的原样。

      【讨论】:

        【解决方案5】:

        为此使用 textView.. 并使用 textview.OnTouchListener 最后 onTouch 改变了文本的颜色.. 就是这样

        或者在你的代码中使用

             android:background="@null"
        

        【讨论】:

        • 我相信还有更多可接受的解决方案
        • android:background="@null" 使用这个..这是唯一的解决方案
        • @snehasish 但它会阻止按钮在按下时突出显示。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-12-05
        • 1970-01-01
        • 1970-01-01
        • 2015-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多