Android 3D按钮文字位置 [英] Android 3D button text position

查看:54
本文介绍了Android 3D按钮文字位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更改不同按钮状态的文本位置?

How to change text position for different button states?

在图像上,您可以看到它的外观:

On image you can see how its look like now:

可绘制

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:state_pressed="true">
        <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
            <item android:top="4dp">
                <shape android:shape="rectangle">
                    <solid android:color="#DCDBDB" />
                    <corners android:radius="7dp" />
                    <stroke android:width="1dp" android:color="#B1B1B1"/>
                </shape>
            </item>

            <item android:bottom="4.5dp" android:left="1.5dp" android:right="1.5dp" android:top="5.5dp">
                <shape android:shape="rectangle" android:gravity="bottom">
                    <solid android:color="#F2F1F1" />
                    <corners android:radius="6dp" />
                    <stroke android:width="1dp" android:color="#FFFFFF"/>
                </shape>
            </item>
        </layer-list>
    </item>

    <item>
        <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
            <item>
                <shape android:shape="rectangle">
                    <solid android:color="#DCDBDB" />
                    <corners android:radius="7dp" />
                    <stroke android:width="1dp" android:color="#B1B1B1"/>
                </shape>
            </item>

            <item android:bottom="8.5dp" android:left="1.5dp" android:right="1.5dp" android:top="1.5dp">
                <shape android:shape="rectangle">
                    <solid android:color="#F2F1F1" />
                    <corners android:radius="6dp" />
                    <stroke android:width="1dp" android:color="#FFFFFF"/>
                </shape>
            </item>
        </layer-list>
    </item>

</selector>

几天前,我问了类似的问题- Android 3D按钮src填充,但是对于文本,我无法使用此技巧:/(或不知道怎么做).

Few days ago I asked similar question - Android 3D button src padding, but for text I cant use this trick :/ (or dont know how).

推荐答案

您将需要创建自定义Button并覆盖更改状态的方法,以便根据当前状态自定义重力.

You will need to create a custom Button and override the methods for changing it's state in order to customize it's gravity based on the current state.

例如,以下是自定义Button的代码,其按下时文本将跳到底部:

For example here is the code for a custom Button whose text will jump to the bottom when it is pressed:

public class VariableGravityButton extends Button {
    public VariableGravityButton(Context context) {
        super(context);
    }

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

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

    @Override
    public void setPressed(boolean pressed) {
        if (pressed != isPressed()) {
            setGravity(pressed ? Gravity.CENTER_HORIZONTAL |
                    Gravity.BOTTOM : Gravity.CENTER);
        }
        super.setPressed(pressed);
    }
}

这篇关于Android 3D按钮文字位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆