不能设置与setGravity重力垂直编程。这是一个Android错误或意见? [英] Can not set gravity with setGravity vertically programatically. Is this an android bug or feature?

查看:768
本文介绍了不能设置与setGravity重力垂直编程。这是一个Android错误或意见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有四个按钮自定义视图。我创建,因为我想动画的按钮在Java视图。我想集中在按钮上的文本,但我可以做到这一点只会水平(左,右),而不是垂直方向。文字粘在上面。

I have a custom View with four buttons. I created the view in Java because I want to animate the buttons. I want to center the text on the buttons, but I can do this only horizontally (left-right) but not vertically. The text sticks to the top.

下面是最基本的code,我能得到工作:

Here is the most basic code I could get working:

public class CopyOfGameView extends ViewGroup implements View.OnClickListener {

    private Main main;

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

    public CopyOfGameView(Context context) {
        super(context);
    }

    public void reset(Main main) {
        this.main = main;
        removeAllViews();

        for (int c = 0; c < 4; c++) {
            ButtonView buttonView = new ButtonView(getContext(), c);
            buttonView.setText("x");
            buttonView.setTextSize(6);
            buttonView.setGravity(Gravity.CENTER);
            addView(buttonView);
        }
    }
        @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int w = getWidth();
        int h = getHeight();
        int oneFourth = h / 4;

        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            ButtonView buttonView = (ButtonView) getChildAt(i);
            int left = 0;
            int top = oneFourth * buttonView.getRow();
            int right = w;
            int bottom = oneFourth * buttonView.getRow() + oneFourth;
            buttonView.layout(left, top, right, bottom);
        }
    }

    public void onClick(View v) {}

    protected ButtonView findButtonView(int row) {
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View v = getChildAt(i);
            if (v instanceof ButtonView) {
                ButtonView buttonView = (ButtonView) v;
                if (buttonView.getRow() == row) {
                    return buttonView;
                }
            }
        }
        return null;
    }

    protected class ButtonView extends Button {
        private int row;

        protected ButtonView(Context context, int row) {
            super(context);
            this.row = row;

            Drawable image = getResources().getDrawable(R.drawable.btn_black);
            setBackgroundDrawable(image);
            setClickable(true);
            setOnClickListener(CopyOfGameView.this);
        }

        public int getRow() {
            return row;
        }

        public void setRow(int row) {
            this.row = row;
        }
    }
}

这是正常的,或这是Android的一个bug?我如何垂直居中文本?

Is this normal or is this a bug in Android? How can I center the text vertically?

推荐答案

该项目是另一个名字复苏,为一年的经验,我能解决这个问题。
我还实施AutoResizeTextView,因为它适合的项目。我把AutoResizeTextView从以下职位:
自动缩放TextView的文本边界内适合

The project was revived under another name and with one year experience I was able to solve the problem. I also implemented AutoResizeTextView, because it fit with the project. I took AutoResizeTextView from the following post: Auto Scale TextView Text to Fit within Bounds

但我必须为这个解决方案适用于onLayout方法测得的尺寸进行修改。但我需要的尺寸测量时让我感动这个逻辑从onLayout到onMeasure。

But I had to modify it as this solution applies the measured dimensions in the onLayout method. But I needed the dimensions at measuring so I moved this logic from onLayout to onMeasure.

我基本上继承ButtonView从LinearLayout中,赋予它一个AutoResizeTextView的孩子,与Android测量(onMeasure,onLayout和布局)播放。

Basically I inherited ButtonView from LinearLayout, gave it an AutoResizeTextView child and played with Android Measuring (onMeasure, onLayout and layout).

记住,我无法使用继承从LinearLyout整个视图大通的解决方案,因为我的动画ButtonViews。

Remember, I could not use Chase's solution of inheriting the whole View from LinearLyout, because I animated the ButtonViews.

有关详细测量:我在CopyOfGameView的onMeasure测量ButtonView,再测AutoResizeTextView在ButtonView的onMeasure。然后在ButtonView的布局方法,我垂直居中TextView的。

Details about measuring: I measured the ButtonView in CopyOfGameView's onMeasure, then measured the AutoResizeTextView in ButtonView's onMeasure. Then in ButtonView's layout method I centered the TextView vertically.

这篇关于不能设置与setGravity重力垂直编程。这是一个Android错误或意见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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