TextView的重力 [英] TextView gravity

查看:96
本文介绍了TextView的重力的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人尝试写他自己实施的的ViewGroup 的,在它的一些TextViews?

我有一个问题,在这样一个实现TextViews不尊重重力属性 TextView.setGravity(Gravity.CENTER)和文本位于左上方角落。

谁能帮我看看为什么?

编辑:

确定。没关系,我理解了它在我自己的了。

如果enyone有兴趣,我只是重写方法 onMeasure()(我所有的TextViews)和 super.onMeasure改变调用() setMeasuredDimension(INT,INT)和重力开始正常工作。

基本上,我的自定义布局我用下面的类来显示文本:

 私有静态类GravityTextView扩展TextView的{

    公共GravityTextView(上下文的背景下){
        超(上下文);
    }

    @覆盖
    保护无效onMeasure(INT widthMeasureSpec,诠释heightMeasureSpec){
        setMeasuredDimension(widthMeasureSpec,heightMeasureSpec);
    }

}
 

解决方案

这是不是错误的行为,你只是想设置了错误的属性。

使用 TextView.setGravity(Gravity.CENTER)要设置TextView的内容的严重性。

您正在寻找的是在<一个href="http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html#attr_android%3alayout_gravity"相对=nofollow> layout_gravity

Has anyone tried to write his own implementation of ViewGroup with some TextViews in it?

I have a problem, that TextViews in such an implementation don't respect the gravity property TextView.setGravity(Gravity.CENTER) and text is positioned in the top left corner.

Can anyone help me find out why?

EDIT:

Ok. nevermind, I figured it out on my own already.

If enyone is interested, I just overwrote method onMeasure() (for all my TextViews) and changed call from super.onMeasure() to setMeasuredDimension(int, int) and gravity started to work normally.

Basically, in my custom layout I use the following class to display text:

private static class GravityTextView extends TextView {  

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

    @Override  
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
        setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);  
    }

}

解决方案

This is not the wrong behavior you are just trying to set the wrong property.

With TextView.setGravity(Gravity.CENTER) you are setting the gravity of the TextView's content.

What you are looking for is the layout_gravity.

这篇关于TextView的重力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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