Textview inside RelativeLayout in custom View not applying gravity correctly?

心已入冬 提交于 2019-12-08 19:53:46

问题


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

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)

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.


回答1:


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.




回答2:


You can set multiple Gravity parameters using setGravity like this.

textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL );

Hava a look on How do I center text horizontally and vertical in a TextView in Android?




回答3:


I just ran into this problem, too. My issue was that when I set gravity on TextView by calling TextView.setGravity(), it silently affects the layout of itself(Textview) in parent view.

Here's what I did for the workaround fix:

TextView schedule = getBubbleTextView (item, hasZeroSpanEvent);
// There's a bug in TextView, we need to use wrapper to fix it   
LinearLayout wrapper = new LinearLayout (getApplicationContext ());
wrapper.addView (schedule); 
hourlyBubbleParent.addView (wrapper, llp);

You should grab the idea that you should use wrapper to workaround this.



来源:https://stackoverflow.com/questions/6222954/textview-inside-relativelayout-in-custom-view-not-applying-gravity-correctly

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