Android 3D button text position

天大地大妈咪最大 提交于 2020-01-01 15:05:21

问题


How to change text position for different button states?

On image you can see how its look like now:

drawable

<?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>

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


回答1:


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.

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);
    }
}


来源:https://stackoverflow.com/questions/21221944/android-3d-button-text-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!