如何在 Android 中使用 textview 显示颠倒的文本? [英] How can you display upside down text with a textview in Android?

查看:21
本文介绍了如何在 Android 中使用 textview 显示颠倒的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Android 中使用 textview 显示颠倒的文本?

How can you display upside down text with a textview in Android?

在我的例子中,我有一个 2 人游戏,他们互相对着玩.我想向面向他们的第二个玩家显示测试.

In my case I have a 2 player game, where they play across from each other. I want to display test to the second player oriented to them.

这是我在 AaronMs 建议后实施的解决方案

This was the solution I implemented after AaronMs advice

执行覆盖的类 bab.foo.UpsideDownText

The class that does the overriding, bab.foo.UpsideDownText

package bab.foo;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.TextView;

public class UpsideDownText extends TextView {

    //The below two constructors appear to be required
    public UpsideDownText(Context context) {
        super(context);
    }

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

    @Override
    public void onDraw(Canvas canvas) {
        //This saves off the matrix that the canvas applies to draws, so it can be restored later. 
        canvas.save(); 

        //now we change the matrix
        //We need to rotate around the center of our text
        //Otherwise it rotates around the origin, and that's bad. 
        float py = this.getHeight()/2.0f;
        float px = this.getWidth()/2.0f;
        canvas.rotate(180, px, py); 

        //draw the text with the matrix applied. 
        super.onDraw(canvas); 

        //restore the old matrix. 
        canvas.restore(); 
    }
}

这是我的 XML 布局:

And this is my XML layout:

<bab.foo.UpsideDownText 
    android:text="Score: 0" 
    android:id="@+id/tvScore" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:textColor="#FFFFFF" 
    >
</bab.foo.UpsideDownText>

推荐答案

在xml文件中添加:

android:rotation = "180"

在相应的元素中颠倒显示文本.

in the respective element to display text upside down.

例如:

<TextView
       android:id="@+id/textView1"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:gravity="center"
       android:text="TextView" 
       android:rotation="180"/>

这篇关于如何在 Android 中使用 textview 显示颠倒的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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