按下时自动更改按钮背景和文本外观(如 iOS)?

发布于 2024-11-16 00:00:29 字数 183 浏览 2 评论 0 原文

在iOS中,如果我将按钮的背景设置为图像,那么当我按下按钮时,按钮的整个内容(包括文本)将被遮挡。我可以在Android中实现相同的效果,还是必须针对不同的状态使用不同的图像?另外,即使我对不同的状态使用不同的图像,如何使文本也带有阴影?一种肮脏的方法是为按钮设置一个 OnClickListener 并在按下时以编程方式隐藏文本,但是还有其他方法吗?

In iOS, if I set a button's background to an image, when I press the button, the whole content of the button (including the text) will be shadowed. Can I achieve the same effect in Android, or do I have to use different images for different states? Also, even if I use different images for different states, how do I make the text also shadowed? A dirty way is to set a OnClickListener to the button and programatically shadow the text when pressed, but are there any other ways?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吃兔兔 2024-11-23 00:00:29

一段时间以来,我一直在尝试寻找解决方案,但找不到任何东西,所以我想出了一个非常简洁的解决方案,适用于所有图像按钮。就像 iOS 一样。

  • 创建一个黑色、10% 透明的图像并将其另存为 PNG。我将其命名为“button_pressed.png”。您可以使用此图像, http://img84.imageshack.us/img84/7924/ buttonpressed.png

  • 创建一个名为相关内容的可绘制对象,我将其称为“button_pressed_layout.xml”

    
    <选择器 xmlns:android="http://schemas.android.com/apk/res/android">
        
    
    
  • 现在将按钮图像放入 LinearLayout 中,然后将 Button 放入 LinearLayout 中。在按钮中使用button_pressed_layout.xml作为背景。

    <前><代码> <按钮
    android:id="@+id/myButtonId"
    android:text="@string/继续"
    安卓:layout_width =“fill_parent”
    安卓:layout_height =“fill_parent”
    android:background="@drawable/button_pressed_layout"
    机器人:文本颜色=“#FFF”
    android:textSize="16dp" >

就是这样!

I´ve been trying to find a solution for this for a while now but couldn´t find anything so I came up with a pretty neat solution which works on all image buttons. Just like iOS.

  • Create a Black, 10% transparent image and save it as PNG. I call it button_pressed.png. You can use this image, http://img84.imageshack.us/img84/7924/buttonpressed.png

  • Create a drawable called something relevant, I call it "button_pressed_layout.xml"

    <?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/button_pressed"  />
    </selector>
    
  • Now put your button image in the LinearLayout and then the Button inside the LinearLayout. In the Button use button_pressed_layout.xml as background.

        <LinearLayout 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/button_image">
            <Button
                android:id="@+id/myButtonId"
                android:text="@string/Continue"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:background="@drawable/button_pressed_layout"
                android:textColor="#FFF"
                android:textSize="16dp" >                   
            </Button>
        </LinearLayout>
    

That´s it!

浅暮の光 2024-11-23 00:00:29

为了在不创建第二个按钮图像的情况下获得按下状态,我创建了一个 OnTouchListener 来更改按钮的 Alpha。这不是最漂亮的按下状态,但给出了有效的自动行为。

public class PressedStateOnTouchListener implements OnTouchListener
{
    PressedStateOnTouchListener( float alphaNormal )
    {
        mAlphaNormal    = alphaNormal;
    }

    public boolean onTouch( View theView, MotionEvent motionEvent )
    {
        switch( motionEvent.getAction() ) {
            case MotionEvent.ACTION_DOWN:
                theView.setAlpha( mAlphaNormal / 2.0f );
                break;

            case MotionEvent.ACTION_UP:
                theView.setAlpha( mAlphaNormal );
                break;
        }

        // return false because I still want this to bubble off into an onClick
        return false;
    }

    private float   mAlphaNormal;
}

在您的 Activity 中,将此侦听器应用于每个按钮:

Button theButton =  (Button)findViewById( R.id.my_button_id );
theButton.setOnTouchListener( new PressedStateOnTouchListener( theButton.getAlpha() ));

To get a pressed-state without creating a 2nd button image, I created an OnTouchListener to change the button's alpha. This is not the most beautiful pressed state, but gives an effective automatic behavior.

public class PressedStateOnTouchListener implements OnTouchListener
{
    PressedStateOnTouchListener( float alphaNormal )
    {
        mAlphaNormal    = alphaNormal;
    }

    public boolean onTouch( View theView, MotionEvent motionEvent )
    {
        switch( motionEvent.getAction() ) {
            case MotionEvent.ACTION_DOWN:
                theView.setAlpha( mAlphaNormal / 2.0f );
                break;

            case MotionEvent.ACTION_UP:
                theView.setAlpha( mAlphaNormal );
                break;
        }

        // return false because I still want this to bubble off into an onClick
        return false;
    }

    private float   mAlphaNormal;
}

In your Activity, apply this listener to each button:

Button theButton =  (Button)findViewById( R.id.my_button_id );
theButton.setOnTouchListener( new PressedStateOnTouchListener( theButton.getAlpha() ));
流云如水 2024-11-23 00:00:29

为了给按钮本身不同的效果,我相信您需要使用自定义图像,如此处,以及不太详细的 这里 至于文字,这篇文章< /a> 看起来它可以解决阴影问题

In order to give the button itself different effects, I believe you need to use custom images, as seen here and, in less detail here As for the text, this post looks like it would do the trick for the shadow

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文