evi*_*one 18 user-interface android button
我为按钮和不同的按钮状态创建了自定义背景.但是现在我达到了一个我无法理解的地步.
当按钮处于正常状态时,它看起来很好.但是当我按下按钮时,我需要将文本向下移动几个像素,因为按钮背景图像移动(实际上感觉就像它在图像上移动一样,因为首先按钮下面有边框,当它处于按下状态时,此边框消失).请看下面的图片.
按下按钮状态时,如何移动按钮中的按钮文本?(可能以某种方式填充或按钮的布局自定义)

在9补丁中设置填充对我来说不起作用.在触摸侦听器中设置填充是杂乱的,在使用按钮的任何地方都会丢失代码.
我选择了子类化Button,结果很合理.在我的情况下,我想偏移icon(drawableLeft)和文本1px左和1px向下.
子类按钮小部件:
package com.myapp.widgets;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Button;
import com.myapp.R;
public class OffsetButton extends Button {
public OffsetButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public OffsetButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public OffsetButton(Context context) {
super(context);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean value = super.onTouchEvent(event);
if (event.getAction() == MotionEvent.ACTION_UP) {
setBackgroundResource(R.drawable.btn_normal);
} else if (event.getAction() == MotionEvent.ACTION_DOWN) {
setBackgroundResource(R.drawable.btn_pressed);
setPadding(getPaddingLeft() + 1, getPaddingTop() + 1, getPaddingRight() - 1,
getPaddingBottom() - 1);
}
return value;
}
}
Run Code Online (Sandbox Code Playgroud)
并在这样的布局中使用它:
<com.myapp.widgets.OffsetButton
android:text="@string/click_me"
android:drawableLeft="@drawable/icon_will_be_offset_too_thats_good" />
Run Code Online (Sandbox Code Playgroud)
几点说明:
我没有使用StateListDrawable的背景,我的代码,而不是切换背景.当我尝试使用时StateListDrawable,填充更改和背景更改之间会有小的暂停.那看起来并不好.
设置背景会重置填充,因此不需要调整填充ACTION_UP以防万一
增加顶部和左侧填充很重要,同时减少底部和右侧填充.因此内容区域的大小保持不变,内容区域实际上只是移位.
| 归档时间: |
|
| 查看次数: |
2869 次 |
| 最近记录: |