Sep*_*phy 27 android vertical-text textview
假设您有一个普通的TextView,其中写有"Stackoverflow",是否可以将TextView旋转-90°,使底部的S和屏幕顶部的W?当然,我可以把我的文字写成图像,旋转它并以这种方式使用它,但我现在对文本很感兴趣.谢谢.
cch*_*son 38
您可以像平常一样设置textview
例如:
<TextView android:id="@+id/txtview"
android:layout_height="fill_parent"
android:layout_width="wrap_content" />
Run Code Online (Sandbox Code Playgroud)
并在你的活动中写一个函数
\n每个字符后然后将文本设置为TextView.
如果您不想插入\n,则必须设置大小android:layout_width并使用字体大小,不要在同一行上放置2个字符且不要截断
编辑 如果我理解正确,您可以使用动画获得所需内容.
例如
下res/anim/myanim.xml:
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="-90"
android:pivotX="50%"
android:duration="0" />
Run Code Online (Sandbox Code Playgroud)
您必须使用此文件来定义文本视图的放置位置.
在您的活动中:
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);
Run Code Online (Sandbox Code Playgroud)
Yur*_*rev 18
为我工作:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
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" />
Run Code Online (Sandbox Code Playgroud)
试试这个.这对我来说可以.它可以垂直显示一行文本,但只能显示一行.颜色,大小,填充,边距和背景都很好.
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);
}
}
Run Code Online (Sandbox Code Playgroud)
如果您使用的是API 11或更高版本,则可以尝试:
TextView t = (TextView) findViewById(R.id.txtview);
String txt = "Stackoverflow";
t.setText(txt);
t.setRotation(90); // 90 degree rotation
Run Code Online (Sandbox Code Playgroud)
我们可以使用XML视图设置Rotation
<TextView android:id="@+id/txtview"
android:rotation="-90"
android:text="123"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
47988 次 |
| 最近记录: |