且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我可以增加一个按钮的onclick面积编程?

更新时间:2023-11-23 09:25:16

我刚刚发现了一个巧妙的方法来解决这个问题。

I have just found a neat way to solve this problem.

  1. 环绕按钮与一个说的LinearLayout具有填充圆形按钮。
  2. 的onclick到的LinearLayout添加相同的按钮。
  3. 在按键设置duplicateParentState为true这使得当你点击该按钮外部,但里面的LinearLayout的按钮突出。

  1. Surround the button with a say a LinearLayout that has the padding round the button.
  2. Add the same onclick to the LinearLayout as the Button.
  3. In the Button set the duplicateParentState to true which make the button highlight when you click outside the button but inside the 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>