【问题标题】:How to center a text above an image when pressed?按下时如何将文本居中放置在图像上方?
【发布时间】:2015-07-27 20:10:46
【问题描述】:

我正在尝试使用 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>

现在,我想在该图像上添加一个文本,该文本将其在图像中的位置更改为在按下时始终位于中心。

按钮正常:

按下按钮:

我该怎么做?

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

提前感谢您的支持!

【问题讨论】:

标签: java android textview imageview imagebutton


【解决方案1】:

选择器的每个状态都有不同的可绘制对象,您走在正确的轨道上(小心 - 您的焦点状态与默认状态相同)。

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

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

【讨论】:

  • 我试过这个,但TextView没有滑下来,它停留在点击背景的中心,与正常的尺寸相同,但给人的印象是2D而不是3D。
【解决方案2】:

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

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

【讨论】:

  • 我试过了,但文本一直停留在按钮的中心,并且不会向下滑动。可能是因为两个背景(正常和按下)具有相同的尺寸?
  • 既然你提到了,我记得padding可能不会根据drawable状态动态变化,所以按下按钮时文本不会自动移动。恐怕做你想做的唯一方法是创建一个自定义类扩展 Button 设置一个 View.OnTouchListener 本身来根据按下状态调整填充。
【解决方案3】:

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

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" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-16
    • 2017-07-04
    • 2014-08-17
    • 2015-09-16
    • 1970-01-01
    • 2022-11-26
    相关资源
    最近更新 更多