Android会在单击时突出显示图像按钮

Dro*_*dme 16 android imagebutton

我正在使用ImageButton.但是点击后我没有得到突出显示.我用Google搜索并且许多建议使用选择器显示另一个图像.有没有办法解决.只使用一个图像并突出显示它或给它一个发光效果.以便用户知道该按钮已被点击.

Zac*_*ese 30

这实际上并不是很难做到.你甚至不需要创建2个单独的.png文件或类似的东西.例如,如果您想要一个具有渐变的按钮,然后在按下按钮时更改它:

第1步:创建默认按钮渐变(drawable/default_button.xml):

<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle">
    <corners android:radius="3dp" />
    <gradient android:endColor="#8ba0bb" android:startColor="#43708f" android:angle="90" />
    <stroke android:width="1dp" android:color="#33364252" />
</shape>
Run Code Online (Sandbox Code Playgroud)

第2步:创建默认按钮按下渐变(drawable/default_button_pressed.xml):

<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle">
    <corners android:radius="3dp" />
    <gradient android:endColor="#43708f" android:startColor="#8ba0bb" android:angle="90" />
    <stroke android:width="1dp" android:color="#33364252" />
</shape>
Run Code Online (Sandbox Code Playgroud)

第3步:创建选择器(drawable/default_button_selector.xml):

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:drawable="@drawable/default_button_pressed" /> 
    <item android:drawable="@drawable/default_button" /> 
</selector>
Run Code Online (Sandbox Code Playgroud)

步骤4 (可选):为按钮创建样式(values/style.xml):

<resources>
    <style name="DefaultButton">
        <item name="android:layout_width">wrap_content</item>   
        <item name="android:layout_height">wrap_content</item>
        <item name="android:background">@drawable/default_button_selector</item>
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

第5步:使用按钮(layout/main.xml):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="fill_parent" android:layout_height="fill_parent">

    <button style="@style/DefaultButton" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

如您所见,这并不是特别困难.

  • 大声笑@Zack`正如你所看到的,做起来并不是特别困难 (22认同)
  • 如果我们需要具有不同背景图像的按钮呢? (4认同)
  • 这很不错,因为您可以使用相同的样式轻松添加许多按钮. (2认同)

Vin*_*tti 19

无需创建多个图像(按下,正常等),甚至不必创建选择器.使用setOnTouchListener而不是setOnClickListener.以下代码将为您提供所点击项目的灰色叠加层.

 ((ImageButton)findViewById(R.id.myImageBtn)).setOnTouchListener(new OnTouchListener() {

      @Override
        public boolean onTouch(View v, MotionEvent event) {
          switch (event.getAction()) {
          case MotionEvent.ACTION_DOWN: {
              ImageButton view = (ImageButton ) v;
              view.getBackground().setColorFilter(0x77000000, PorterDuff.Mode.SRC_ATOP);
              v.invalidate();
              break;
          }
          case MotionEvent.ACTION_UP:

              // Your action here on button click

          case MotionEvent.ACTION_CANCEL: {
              ImageButton view = (ImageButton) v;
              view.getBackground().clearColorFilter();
              view.invalidate();
              break;
          }
          }
          return true;
        }
    });
Run Code Online (Sandbox Code Playgroud)

  • 这对我有用。确保也清除 ACTION_UP 上的滤色器。 (2认同)

Fra*_*cis 6

您可以简单地使用android:foreground您的来View实现可点击的效果:

android:foreground="?android:attr/selectableItemBackground"
Run Code Online (Sandbox Code Playgroud)


要与深色主题一起使用,请将主题添加到您的layout(以使可点击效果清晰):

android:theme="@android:style/ThemeOverlay.Material.Dark"
Run Code Online (Sandbox Code Playgroud)