简体   繁体   English

Android Button可绘制问题

[英]Android Button drawable issue

I'm trying to achieve buttons similar to the icons in the Action Bar, ie transparent images that change background colour on click. 我正在尝试实现与操作栏中的图标类似的按钮,即,透明图像会在单击时更改背景颜色。

These are the conditions I'd like to satisfy: 这些是我要满足的条件:

  1. Background colour changes on click 点击时背景颜色发生变化
  2. Contains a small rectangle shape in the center 中心包含一个小矩形
  3. Rectangle can change colour programmatically 矩形可以通过程序更改颜色

I tried using a Drawable to represent the rectangle and then to set it as the background of the button, but it expanded to the edges of the button and so there was no background colour to change when clicked (I was able to use drawable.setColorFilter() and button.setBackground(drawable) to alter its colour however). 我尝试使用Drawable表示矩形,然后将其设置为按钮的背景,但是它扩展到了按钮的边缘,因此单击时没有背景色发生变化(我可以使用drawable.setColorFilter ()和button.setBackground(drawable)更改其颜色)。 Shrinking the button also shrank the touch-target. 缩小按钮也会缩小触摸目标。

I also tried using a StateListDrawable containing two rectangle shapes, a background and an inner rectangle, so on state_pressed the background rectangle would change colour. 我使用也尝试StateListDrawable包含两个矩形形状,一个背景和内矩形,等等state_pressed矩形背景会改变颜色。 However the front rectangle stretched again and completely covered he background rectangle. 但是,前矩形再次拉伸并完全覆盖了背景矩形。

Which method can achieve my conditions? 哪种方法可以达到我的条件? Thanks. 谢谢。

Have 2 images for the state of the buttons and use this: 有2张按钮状态的图像,并使用此图像:

        boolean clicked = false
        Button btn = (Buttton)findViewById(R.id.button);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) 
            {
                if (clicked) 
                {
                    clicked = false;
                    btn.setBackgroundResource(R.drawable.button_image_on);
                }

                else
                {
                    clicked = true;
                    btn.setBackgroundResource(R.drawable.button_image_off);
                }
            }
        });

This talk from Google IO 2013 on Android UI design by Nick Butcher and Roman Nurik helped to solve the problem. 尼克·巴彻(Nick Butcher)和罗曼·努里克(Roman Nurik)在Google IO 2013上关于Android UI设计的演讲有助于解决该问题。

The XML attribute style="?android:borderlessButtonStyle" can be set on an ImageButton (or even just a Button ) to give it a transparent background which lights up in a standard Holo blue colour on touch. XML属性style="?android:borderlessButtonStyle"可以在ImageButton (或什至只是Button )上设置,以为其提供透明背景,并在触摸时以标准Holo蓝色点亮。 According to Roman Nurik, this attribute provides the standard styling "all for free". 根据Roman Nurik的说法,此属性提供“全部免费”的标准样式。

I created an ImageButton containing that borderless button attribute in my layout.xml file, then I created my coloured Drawable in Java and put it inside the button using myImageButton.setImageDrawable(myColouredDrawable); 我在layout.xml文件中创建了一个包含无边界按钮属性的ImageButton ,然后用Java创建了彩色的Drawable ,然后使用myImageButton.setImageDrawable(myColouredDrawable);将其放入按钮中myImageButton.setImageDrawable(myColouredDrawable); to fit all three of my conditions. 符合我所有的三个条件。

Edit July 2013: To bring the ActionBar pattern to older versions of Android using Jake Wharton's ActionBarSherlock, use style="@style/Widget.Sherlock.ActionButton" , or if using the ActionBarCompat Support Library, use style="@style/Widget.AppCompat.ActionButton" 2013年7月修改:要使用Jake Wharton的ActionBarSherlock将ActionBar模式带到旧版Android,请使用style="@style/Widget.Sherlock.ActionButton" ;如果使用ActionBarCompat支持库,请使用style="@style/Widget.AppCompat.ActionButton"

Update May 2017: For ImageViews (I believe it is actually an ImageViewCompat ) I am using android:background="?android:attr/selectableItemBackground" on Jellybean onwards. 2017年5月更新:对于ImageViews(我相信它实际上是一个ImageViewCompat ),我开始在Jellybean上使用android:background="?android:attr/selectableItemBackground"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM