Digamos que tiene una vista de texto normal, con "Stackoverflow" escrito en ella, ¿es posible girar la vista de texto en -90 °, para tener la S en la parte inferior y la W en la parte superior de la pantalla? Por supuesto, podría escribir mi texto como una imagen, rotarlo y usarlo de esa manera, pero ahora estoy interesado en el texto. Gracias.¿Es posible escribir verticalmente en una vista de texto en Android?
Respuesta
Puede configurar el TextView como lo haría normalmente
por ejemplo:
<TextView android:id="@+id/txtview"
android:layout_height="fill_parent"
android:layout_width="wrap_content" />
y escribir una función en su actividad para
- revertir los caracteres del texto
- insertar
\n
después de cada caracteres
y luego configure el texto en TextView.
Si no desea insertar el \n
, tendrá que ajustar el tamaño de android:layout_width
y jugar con el tamaño de fuente que no tienen 2 personajes encajan en la misma línea y sin truncamiento
Editar Si yo Te he entendido bien, puedes obtener lo que quieres usando animación.
Por ejemplo
Bajo res/animation/myanim.xml
:
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="-90"
android:pivotX="50%"
android:duration="0" />
Tendrá que jugar con este archivo para definir dónde quiere que su vista de texto a ser colocado.
En su actividad:
TextView t = (TextView)findViewById(R.id.txtview);
String txt = "Stackoverflow";
t.setText(txt);
RotateAnimation ranim = (RotateAnimation)AnimationUtils.loadAnimation(this, R.anim.myanim);
ranim.setFillAfter(true); //For the textview to remain at the same place after the rotation
t.setAnimation(ranim);
Sí, a pesar de eso, pero eso no es lo que se explica y pregunté en realidad, no quiero que cada personaje sea uno encima del otro, pero todos con una rotación de -90 ° ... ¿es esto posible? – Sephy
aha bien - Creo que sé lo que quieres decir ahora - No tengo ni idea de cómo hacerlo, pero lo pensaré – ccheneson
Sí, es una gran idea. – Sephy
probar esto. Funciona bien para mí. Puede mostrar una línea de texto verticalmente, pero solo una línea. los colores, el tamaño, los almohadones, los márgenes y el fondo funcionan bien.
public class VerticalTextView extends TextView {
public VerticalTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public VerticalTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public VerticalTextView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
final ColorStateList csl = getTextColors();
final int color = csl.getDefaultColor();
final int paddingBottom = getPaddingBottom();
final int paddingTop = getPaddingTop();
final int viewWidth = getWidth();
final int viewHeight = getHeight();
final TextPaint paint = getPaint();
paint.setColor(color);
final float bottom = viewWidth * 9.0f/11.0f;
Path p = new Path();
p.moveTo(bottom, viewHeight - paddingBottom - paddingTop);
p.lineTo(bottom, paddingTop);
canvas.drawTextOnPath(getText().toString(), p, 0, 0, paint);
}
}
Si está utilizando API 11 o posterior, puede intentar:
TextView t = (TextView) findViewById(R.id.txtview);
String txt = "Stackoverflow";
t.setText(txt);
t.setRotation(90); // 90 degree rotation
, esto tendrá problemas con los límites de la vista de texto. onMeasure() debería redefinirse para él. –
Creo que esto no es un problema, ¿o sí? Simplemente cambie de ancho y alto ... – Chris623
trabajado para mí:
public class VerticalTextView extends TextView {
private int _width, _height;
private final Rect _bounds = new Rect();
public VerticalTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public VerticalTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public VerticalTextView(Context context) {
super(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// vise versa
_height = getMeasuredWidth();
_width = getMeasuredHeight();
setMeasuredDimension(_width, _height);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.save();
canvas.translate(_width, _height);
canvas.rotate(-90);
TextPaint paint = getPaint();
paint.setColor(getTextColors().getDefaultColor());
String text = text();
paint.getTextBounds(text, 0, text.length(), _bounds);
canvas.drawText(text, getCompoundPaddingLeft(), (_bounds.height() - _width)/2, paint);
canvas.restore();
}
private String text() {
return super.getText().toString();
}
}
xml:
<VerticalTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:background="@color/feedback_background"
android:padding="4dip"
android:text="@string/feedback"
android:textColor="@color/feedback_text_color"
android:textSize="@dimen/text_xlarge" />
Sé que no es gran cosa, pero si algún día algunos n00b como yo intentan implementarlo y obtienen algunos errores, intente usar: '
Funciona perfectamente. ¡Gracias! – Lancelot
¿Cómo rotar a 90 grados en lugar de -90? – Dharmendra
podemos establecer Rotación con vista XML
<TextView android:id="@+id/txtview"
android:rotation="-90"
android:text="123"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
esto funciona pero se vuelve muy difícil usar el editor de interfaz de usuario luego – Louis
Proporcioné una solución en otro StackOverflow question. Puede obtener TextView vertical al extender desde Ver y anular sus métodos onMeasure()
y onDraw()
. Sin embargo, no admitirá todas las funciones de TextView, sino las principales, como el relleno, el tamaño, el color y la fuente.
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.os.Build;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class VerticalLabelView extends View
{
private final String LOG_TAG = "VerticalLabelView";
private final int DEFAULT_TEXT_SIZE = 30;
private int _ascent = 0;
private int _leftPadding = 0;
private int _topPadding = 0;
private int _rightPadding = 0;
private int _bottomPadding = 0;
private int _textSize = 0;
private int _measuredWidth;
private int _measuredHeight;
private Rect _textBounds;
private TextPaint _textPaint;
private String _text = "";
private TextView _tempView;
private Typeface _typeface = null;
private boolean _topToDown = false;
public VerticalLabelView(Context context)
{
super(context);
initLabelView();
}
public VerticalLabelView(Context context, AttributeSet attrs)
{
super(context, attrs);
initLabelView();
}
public VerticalLabelView(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
initLabelView();
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public VerticalLabelView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
{
super(context, attrs, defStyleAttr, defStyleRes);
initLabelView();
}
private final void initLabelView()
{
this._textBounds = new Rect();
this._textPaint = new TextPaint();
this._textPaint.setAntiAlias(true);
this._textPaint.setTextAlign(Paint.Align.CENTER);
this._textPaint.setTextSize(DEFAULT_TEXT_SIZE);
this._textSize = DEFAULT_TEXT_SIZE;
}
public void setText(String text)
{
this._text = text;
requestLayout();
invalidate();
}
public void topToDown(boolean topToDown)
{
this._topToDown = topToDown;
}
public void setPadding(int padding)
{
setPadding(padding, padding, padding, padding);
}
public void setPadding(int left, int top, int right, int bottom)
{
this._leftPadding = left;
this._topPadding = top;
this._rightPadding = right;
this._bottomPadding = bottom;
requestLayout();
invalidate();
}
public void setTextSize(int size)
{
this._textSize = size;
this._textPaint.setTextSize(size);
requestLayout();
invalidate();
}
public void setTextColor(int color)
{
this._textPaint.setColor(color);
invalidate();
}
public void setTypeFace(Typeface typeface)
{
this._typeface = typeface;
this._textPaint.setTypeface(typeface);
requestLayout();
invalidate();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
try
{
this._textPaint.getTextBounds(this._text, 0, this._text.length(), this._textBounds);
this._tempView = new TextView(getContext());
this._tempView.setPadding(this._leftPadding, this._topPadding, this._rightPadding, this._bottomPadding);
this._tempView.setText(this._text);
this._tempView.setTextSize(TypedValue.COMPLEX_UNIT_PX, this._textSize);
this._tempView.setTypeface(this._typeface);
this._tempView.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
this._measuredWidth = this._tempView.getMeasuredHeight();
this._measuredHeight = this._tempView.getMeasuredWidth();
this._ascent = this._textBounds.height()/2 + this._measuredWidth/2;
setMeasuredDimension(this._measuredWidth, this._measuredHeight);
}
catch (Exception e)
{
setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
Log.e(LOG_TAG, Log.getStackTraceString(e));
}
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
if (!this._text.isEmpty())
{
float textHorizontallyCenteredOriginX = this._measuredHeight/2f;
float textHorizontallyCenteredOriginY = this._ascent;
canvas.translate(textHorizontallyCenteredOriginY, textHorizontallyCenteredOriginX);
float rotateDegree = -90;
float y = 0;
if (this._topToDown)
{
rotateDegree = 90;
y = this._measuredWidth/2;
}
canvas.rotate(rotateDegree);
canvas.drawText(this._text, 0, y, this._textPaint);
}
}
}
- 1. ¿Es posible enmascarar una vista en Android?
- 2. una vista de texto en Android
- 3. ¿Es posible escribir texto en richtextbox en tiempo de diseño?
- 4. Android: ¿Es posible hacer una copia de una Vista?
- 5. ¿Es posible colocar una vista sobre otra en Android?
- 6. Centrar verticalmente texto excesivamente grande en los botones de Android
- 7. ¿Es posible escribir texto personalizado en Google Maps API v3?
- 8. Centrar verticalmente un texto en una fila de vista de lista
- 9. Alinear texto verticalmente en Navbar
- 10. Cómo hacer zoom en una vista de texto en android?
- 11. Hacer una vista de texto de hipervínculo en android
- 12. Vista de desplazamiento para vista de texto en android
- 13. Texto parpadeante en la vista de Android
- 14. haciendo una vista de texto hacer clic en Android
- 15. Android ¿Agregar imagen a texto (en vista de texto)?
- 16. Mostrar texto dinámico en una vista de Android
- 17. ¿Es posible escribir frases sencillas en Python?
- 18. Android: Vista de Texto hipervínculo
- 19. ¿Es posible deshabilitar el desplazamiento en una lista Vista?
- 20. ¿Es posible escribir un firewall en python?
- 21. ¿Es posible insertar datos en una vista de MySQL?
- 22. ¿Es posible escribir código para escribir código?
- 23. ¿Es posible escribir un cliente de Skype?
- 24. ¿Es posible deshabilitar todas las funciones de desplazamiento y acercamiento en una vista web de Android?
- 25. Seguimiento del texto seleccionado en la vista web de Android
- 26. Android: ¿Es posible actualizar solo un elemento en una vista de lista?
- 27. alineando texto verticalmente en la fila NSTableView
- 28. Cómo centrar drawableTop y texto verticalmente en el botón en Android?
- 29. ¿Es posible escribir sintaxis como -()()?
- 30. ¿Es posible actualizar parcialmente una vista materializada en Oracle?
Es posible hacer esto ahora en XML: http://stackoverflow.com/questions/3774770/sideways-view-with-xml-android – chobok