【问题标题】:TextViews Gravity does not work verticallyTextViews Gravity 不能垂直工作
【发布时间】:2013-04-10 13:25:22
【问题描述】:

当我将 TextView 的 DrawingCache 绘制到另一个 View 的 Canvas 时,TextView 的重力在垂直方向上没有影响。

这里的类将 TextViews 画布绘制到自己的画布上:

public class GravityDecorator extends View{
    private View view;
    private Paint paint= new Paint();

    public GravityDecorator(View view,Context context) {
        super(context);
        this.view = view;
        view.setDrawingCacheEnabled(true);
        view.layout(0, 0,600,500);
        this.layout(0, 0,600,500);
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) { 
        super.onDraw(canvas);
        view.buildDrawingCache();       
        canvas.drawBitmap(view.getDrawingCache(), 0, 0, paint);     
        view.destroyDrawingCache();
    }

}

这是测试它的代码(onCreate):

    ViewGroup root = (ViewGroup) findViewById(R.id.root); // is a linear_layout - width and height is match_parent
    TextView tv = new TextView(getApplicationContext());
    tv.setText("Hello World!");
    tv.setTextSize(40.0f);      
    tv.setLayoutParams(new LinearLayout.LayoutParams(300,200));     
    tv.setTextColor(Color.WHITE);
    tv.setBackgroundColor(Color.parseColor("#3131c5"));
    tv.setGravity(Gravity.CENTER);

    GravityDecorator gd = new GravityDecorator(tv, getApplicationContext());
    root.addView(gd);

如你所见,TextViews 内容的 Gravity 只在水平方向生效。

这是什么原因以及如何解决这个问题,如果它是一个错误?

谢谢

【问题讨论】:

    标签: android canvas textview gravity drawingcache


    【解决方案1】:
    root = (ViewGroup) findViewById(R.id.root); // is a linear_layout - width and height is match_parent
    tv = new TextView(getApplicationContext());
    tv.setText("Hello World!");
    tv.setTextSize(40.0f);      
    tv.setLayoutParams(new LinearLayout.LayoutParams(300,200));     
    tv.setTextColor(Color.WHITE);
    tv.setBackgroundColor(Color.parseColor("#3131c5"));
    tv.setGravity(Gravity.CENTER);
    tv.invalidate();
    root.addView(tv);
    GravityDecorator gd = new GravityDecorator(tv, getApplicationContext());
    root.addView(gd);
    

    可能是因为最初没有为 TextView 设置布局参数。尝试将视图添加到Parent,然后获取drawingCache

    public class GravityDecorator extends View{
        private View view;
        private Paint paint= new Paint();
    
        public GravityDecorator(View view,Context context) {
            super(context);
            this.view = view;
            view.setDrawingCacheEnabled(true);
            view.layout(0, 0,600,500);
            this.layout(0, 0,600,500);
            invalidate();
        }
    
        @Override
        protected void onDraw(Canvas canvas) { 
            super.onDraw(canvas);
            view.buildDrawingCache();      
    
            Bitmap bmp = view.getDrawingCache();
    
            canvas.drawBitmap(bmp, 0, 0, paint);     
            view.destroyDrawingCache();
            if(root.indexOfChild(tv) != -1)
                root.removeView(tv);
    
    
        }
    
    }
    

    【讨论】:

    • 这将在中心绘制 TextView 的内容,但 Size 现在就像“wrap_content”。我想实现一个类,它用边框(顶部、左侧、右侧、底部或圆形)装饰每个视图。所以这里的这个类只是演示行为的例子,没有所有其他代码。我的班级用边框装饰视图,包括填充和边距 - 但重力不是。
    • 您获得的位图与您在布局参数 tv.setLayoutParams(new LinearLayout.LayoutParams(300,200))内提供的高度完全相同;
    • 哦,好的。我错过了。非常感谢!
    • 除了添加和删除视图还有其他选择吗?
    猜你喜欢
    • 2013-07-17
    • 1970-01-01
    • 1970-01-01
    • 2015-07-30
    • 2012-12-04
    • 1970-01-01
    • 2022-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多