我可以通过编程方式增加按钮onclick-area吗?

pgs*_*rom 28 android button

有时我的UI中有一个按钮,它很小,很难点击.到目前为止我的解决方案是在photoshop中的按钮周围添加透明边框.只是增加按钮上的填充不起作用,因为这也会拉伸图像.因为每次想要更改可点击表面时打开photoshop都有点大惊小怪,有没有办法以编程方式执行此操作?我已经尝试在按钮后面放置一个framelayout并使其可以点击,但是按钮不会改变触摸时的外观.当然我还可以在framelayout上添加一个ontouchlistener来改变按钮的外观,但是如果我有几个这样的按钮,它就会有一些代码.

干杯,

小智 34

我个人而言,我会使用TouchDelegate.这使您可以处理触摸目标,视觉视图可以作为两个不同的东西.非常便利...

http://developer.android.com/reference/android/view/TouchDelegate.html

  • 关于`TouchDelegate`和代码片段的解释:http://developer.android.com/training/gestures/viewgroup.html#delegate (3认同)

小智 28

我刚刚找到了解决这个问题的简洁方法.

  1. 使用一个说明为LinearLayout的按钮环绕按钮,按钮周围有填充.
  2. 将相同的onclick作为Button添加到LinearLayout.
  3. 在Button中,将duplicateParentState设置为true,当您在按钮外部但在LinearLayout内部单击时,该按钮会突出显示该按钮.

    <LinearLayout
        android:layout_height="fill_parent"
        android:onClick="searchButtonClicked" 
        android:layout_centerVertical="true" 
        android:orientation="horizontal" 
        android:layout_width="wrap_content" 
        android:paddingRight="10dp" 
        android:paddingLeft="30dp">
        <Button 
            android:id="@+id/search_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/toggle_button_selector"
            android:textColor="#fff"
            android:text="Search"
            android:focusable="true"
            android:textStyle="bold"
            android:onClick="searchButtonClicked" 
            android:layout_gravity="center_vertical" 
            android:duplicateParentState="true"/>
    </LinearLayout>
    
    Run Code Online (Sandbox Code Playgroud)

  • 这只适用于.要使其完全正常工作,您需要使按钮setClickable(false),否则单击botton本身将不会突出显示,因为它从此时未单击的父项获取其状态.一旦使按钮不可单击,就不需要在其上设置onClick侦听器. (6认同)

ant*_*omo 12

这是一个非常晚的"我也是",但在谈到这个以及寻找解决方案的其他问题之后,我找到了一个简单,优雅的解决方案.

另一个问题是他们的图像透明背景无法点击.如果这是一个问题,这似乎也解决了这个问题.

这是按钮:

<ImageButton
    android:id="@+id/arrowUp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/arrow_up"
    android:background="@drawable/clear_button_background" />
Run Code Online (Sandbox Code Playgroud)

相关的行是最后两行."@drawable/arrow_up"是一个可绘制的*.xml文件中定义的*.png文件中的几个按钮状态,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
 <item android:state_pressed="true"
       android:drawable="@drawable/tri_up_blue" /> <!-- pressed -->
 <item android:state_selected="true"
       android:drawable="@drawable/tri_up_blue" /> <!-- selected -->
 <item android:state_focused="true"
       android:drawable="@drawable/tri_up_blue" /> <!-- focused -->
 <item android:drawable="@drawable/tri_up_green" /> <!-- default -->
</selector>
Run Code Online (Sandbox Code Playgroud)

只是你的基本按钮.没什么特别的.而"@drawable/clear_button_background"仅仅是这样的:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"  
    android:shape="rectangle">
   <solid android:color="@android:color/transparent"/>
   <size android:width="20dp" android:height="30dp" />
</shape>
Run Code Online (Sandbox Code Playgroud)

这里的高度和宽度是可点击区域,根据需要调整大小.与荒谬细致的TouchDelegate不同,您可以在单个视图中根据需要重复使用此按钮.没有额外的听众.它不会向您的层次结构中添加任何视图或组,并且您不会整天使用填充和边距.

简单.优雅.