在Android上使无边框图像按钮变小

时间:2016-09-08 13:33:00

标签: android button borderless

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

<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"/>

结果按钮是无边框的,但在交叉图像周围有很多空的位置(按钮被截屏以使空白区域可见)
http://image.prntscr.com/image/db59d5f85f2e4e6399175e589a8d455b.png
我该如何解决?

5 个答案:

答案 0 :(得分:3)

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

结果
http://image.prntscr.com/image/e208ef28ff084a108e59a37f114562fd.png
另外,我会使用负边距将按钮放在靠近角落的位置。

答案 1 :(得分:3)

您应该使用:

  android:background="?selectableItemBackgroundBorderless"

答案 2 :(得分:0)

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

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

android:background="@null"

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

答案 3 :(得分:0)

随着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,否则用户将很难尝试触摸它。

如果你真的需要它看起来更接近视图的边缘,你总是可以使用一个不居中的图像(在一侧或两侧有透明填充),但我会尽量避免这样做这个解决方法,并尝试重新考虑我的应用程序的设计,以适应按钮原样。

答案 4 :(得分:-1)

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

或者在您的代码中使用

     android:background="@null"