EditText高度变化的麻烦

时间:2015-05-07 16:32:44

标签: android android-edittext

我有动态改变身高的char* str = "Hello, World"。所以编辑文本时遇到问题..文本被隐藏,请看截图: enter image description here

代码:

EditText

那么,我做错了什么?

布局:

final EditText editText = new EditText(getContext());
editText.setHint("?");

editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxAnswerLength.length())});
float measureText = editText.getPaint().measureText(maxAnswerLength);
editText.setWidth(editText.getPaddingLeft() + editText.getPaddingRight() + (int) measureText);
editText.setGravity(Gravity.CENTER);
editText.setTextSize(TEXT_SIZE);
editText.setSingleLine(true);

addView(editText);

editText.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

    private int measuredHeight;

    @Override
    public void onGlobalLayout() {
        if (measuredHeight == 0) {
            ViewGroup.LayoutParams layoutParams = editText.getLayoutParams();
            measuredHeight = editText.getMeasuredHeight();
            layoutParams.height = measuredHeight / 2;
            editText.setLayoutParams(layoutParams);
        }
    }
});

1 个答案:

答案 0 :(得分:0)

谢谢大家。我找到了解决这个问题的方法:

editText.setPadding(-5, -5, -5, -3);

感谢。