按下时如何将图像置于图像上方?

时间:2015-07-27 20:10:47

标签: java android textview imageview imagebutton

我正在尝试使用Android Studio编写应用程序,我希望自定义背景的按钮在按下时会发生变化。我的实际代码是:

<?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/new_game_button_pressed" />
    <item android:state_focused="true"
        android:drawable="@drawable/new_game_button" />
    <item android:drawable="@drawable/new_game_button" />
</selector>

现在,我希望在该图像上有一个文本,可以更改其在图像中的位置,以便在按下时始终适合中心。

按钮正常:

enter image description here

按下按钮:

enter image description here

我该怎么做?

从现在开始我一直在使用ImageButtons,但它不会派上用场,因为我必须为每个按钮制作两个文件(正常和按下),对其中的文本进行硬编码,然后为每种语言制作另外两个文件我想要。

提前感谢您的支持!

3 个答案:

答案 0 :(得分:0)

您使用的选择器位于正确的轨道上,每个状态都有不同的绘图(小心 - 您的焦点状态与默认状态相同)。

您可以使用常规TextView,并在其上设置背景。然后,当你在TextView上设置了一个点击监听器时,它会在你按下它时改变背景。

对于要使用的state_focused,您必须使TextView成为焦点。

答案 1 :(得分:0)

使用带有自定义背景drawable的Button,类似于您的选择器。按钮显示居中的文本,默认情况下是可聚焦的。

如果您的背景图片是九个补丁,则它们可能包含填充信息。使用正确的填充信息,文本将正确居中: http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch

答案 2 :(得分:0)

我解决了这个问题,创建了另一个继承自Button 1的类:

package com.mycompany.myapp.widgets;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.Button;
import android.view.MotionEvent;

import com.mycompany.myapp.R;

public class BlueOffsetButton extends Button {

    public BlueOffsetButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public BlueOffsetButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public BlueOffsetButton(Context context) {
        super(context);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        boolean value = super.onTouchEvent(event);

        //If not clickable
        if(!this.isClickable()){
            //Do nothing
            return value;
        }

        //If clickable

        //If the button needs to go up (released)
        if (event.getAction() == MotionEvent.ACTION_UP) {
            //Set the proper background
            setBackgroundResource(R.drawable.blue_button_normal);
            //Restore the initial padding (0, 0, 0, 0)
            setPadding(getPaddingLeft(), getPaddingTop() - 10, getPaddingRight(), getPaddingBottom() + 10);
        }

        //If the button needs to go down (pressed)
        else if (event.getAction() == MotionEvent.ACTION_DOWN) {
            //Set the proper background
            setBackgroundResource(R.drawable.blue_button_pressed);
            //Set the proper padding to center the text (0, 10, 0, -10)
            setPadding(getPaddingLeft(), getPaddingTop() + 10, getPaddingRight(), getPaddingBottom() - 10);
        }

        return value;
    }
}

然后在我需要按钮的地方使用它:

<com.mycompany.myapp.widgets.BlueOffsetButton
    style="@style/BlueButton"
    android:id="@+id/newGameButton"
    android:layout_above="@+id/settingsButton"
    android:text="@string/new_game_button_text"
    android:onClick="onClickNewGame" />