TextView的内部RelativeLayout的自定义视图中不能正确运用重力? [英] Textview inside RelativeLayout in custom View not applying gravity correctly?

查看:169
本文介绍了TextView的内部RelativeLayout的自定义视图中不能正确运用重力?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在那里我创造我自己的观点,我加入了一些TextViews到它的设置。然而,重力设置打破了它。 (据水平中心,而不是垂直方向)我做的这种方式,因为还有其他的东西,我也有我的视图中绘制除了刚才的TextViews,但那些做工精细。这里只有与TextView的重力问题。下面是我有什么偏code。

So I have a setup where I'm creating my own View and I'm adding some TextViews into it. However, the gravity setting is broken for it. (It centers horizontally, but not vertically) I'm doing it this way because there's other stuff I'm also drawing within my view besides just the TextViews, but those work fine. There's only a problem with the TextView gravity. Here's partial code of what I have.

public class myView extends View {

    protected RelativeLayout baseLayout;
    protected TextView textView1;
    protected TextView textView2;

    public myView (Context context) {
        super(context);
        setLayoutParams(new LayoutParams(FILL_PARENT, FILL_PARENT));

        baseLayout = new RelativeLayout(context);
        baseLayout.setLayoutParams(new LayoutParams(FILL_PARENT, FILL_PARENT));

        textView1 = new TextView(context);
        // initialize textView1 string, id, textsize, and color here
        textView2 = new TextView(context);
        // initialize textView2 string, id, textsize, and color here

        baseLayout.addView(textView1);
        baseLayout.addView(textView2);
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Resources res = getResources();
        // calculate out size and position of both textViews here
        textView1.layout(left1, top1, left1 + width1, top1 + height1);
        textView1.setGravity(Gravity.CENTER);
            textView1.setBackgroundColor(green); // just to make sure it's drawn in the right spot
        textView2.layout(left2, top2, left2 + width2, top2 + height2);
        textView2.setGravity(Gravity.CENTER);
            textView2.setBackgroundColor(blue); // same as above

        baseLayout.draw(canvas);
    }
}

这绘制的确切地点,而我希望他们(我知道,因为背景色)大小的TextViews,但重力设置他们水平只能居中..不是垂直。 (是的,TextViews比实际文本字符串大)

This draws the TextViews in the exact spots and sizes that I want them (I know because of the background color), but the gravity sets them to be only centered horizontally.. not vertically. (yes, the TextViews are larger than the actual text strings)

我大概可以实现在此找到了解决办法(的TextView重力),但是这似乎并不像一个非常有效或可靠的方法来解决这个问题。是不是我做错了是造成重力停止正常工作?任何输入/帮助AP preciated。

I could PROBABLY implement the solution found here (TextView gravity), but that doesn't seem like a very efficient or reliable way to get around this. Is there something I'm doing wrong that's causing gravity to stop working correctly? Any input/help is appreciated.

推荐答案

好吧..所以我想通了这一点。我刚上运行的每个TextView的措施()方法..所以我的新的code如下:

Ok.. So I figured this out. I just had to run the measure() method on each TextView.. So my new code looks like:

    textView1.measure(MeasureSpec.makeMeasureSpec(width1, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height1, MeasureSpec.EXACTLY));
    textView1.layout(left1, top1, left1 + width1, top1 + height1);
    textView1.setGravity(Gravity.CENTER);

    textView2.measure(MeasureSpec.makeMeasureSpec(width2, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height2, MeasureSpec.EXACTLY));
    textView2.layout(left2, top2, left2 + width2, top2 + height2);
    textView2.setGravity(Gravity.CENTER);

现在它的水平和垂直像它应该中心。如果您遇到同样的问题,尝试了这一点。

Now it centers both horizontally and vertically like it should. If you're having the same issues, try this out.

这篇关于TextView的内部RelativeLayout的自定义视图中不能正确运用重力?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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