我在Graph页面中有一种情况,LinearLayout
应该显示TextView
旋转90度。如何旋转TextView 90度并显示
回答
最快,最方便的方法是通过Rotate
您的定期的TextView像这样Animation
使用旋转动画。
rotateAnimation.xml:
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="-90"
android:pivotX="50%"
android:duration="0"
android:fillAfter="true" />
Java代码:
TextView text = (TextView)findViewById(R.id.txtview);
text.setText("rotated text here");
RotateAnimation rotate= (RotateAnimation)AnimationUtils.loadAnimation(this,R.anim.rotateAnimation);
text.setAnimation(rotate);
这是好的,但没有帮助我很多检查这个http://stackoverflow.com/questions/1258275/vertical-rotated-label -in-android在我的cse中帮了我很多 – sakshi
Android中的任何新观点有一个叫setRotation(float)方法,你可以用它
textview.setRotation(float);
但请请注意,此方法在API级别11
中添加,所以如果你想支持它,你可以使用这个
if (Build.VERSION.SDK_INT < 11) {
RotateAnimation animation = new RotateAnimation(oldAngel, newAngel);
animation.setDuration(100);
animation.setFillAfter(true);
watermarkText.startAnimation(animation);
} else {
watermarkText.setRotation(progress);
}
多一点解释会很好。 –
我编辑答案的更多信息 –
试试这个::
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:rotation="-95"
android:text="2"
/>
供参考。它需要Build.VERSION.SDK_INT> 11 – MilapTank
这个方法的问题是宽度大小没有改变。任何想法如何克服这一点? – abh22ishek
[解决]一年这是我遗愿清单上没有任何合适的后我终于在论坛上回答了这个问题!
这里的技巧是硬编码TextView
的layout_width和layout_height大于文本的大小 - 例如, 100dp x 100dp。
然后将其放置在一个FrameLayout
的TextView
,并打开关断限幅为FrameLayout
android:clipChildren="false"
和夹到填充关闭TextView的android:clipToPadding="false"
:
TextView
with hard-coded height and width
的TextView
现在将漂浮在FrameLayout
。使用重力设置来移动边界内的文本。
然后使用layout_gravity设置在其内的父FrameLayout
内移动边界:
这里是通过-90度旋转的右侧和底部对齐的文本的例子:
[UPDATE]示例XML用于帧视图壳体旋转文本:
<FrameLayout
android:layout_width="@dimen/item_col_width_val"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:padding="@dimen/table_header_margin">
<TextView
android:layout_width="@dimen/table_title_row_height"
android:layout_height="@dimen/table_title_row_height"
android:layout_gravity="bottom|end"
android:gravity="bottom"
android:rotation="-90"
android:text="@string/asm_col_title_in_stock"
android:textColor="@color/colorGood" />
</FrameLayout>
请分享您的XML代码,以便我们更快地理解您对答案的解释。 – ProgDevCode
@ProgDevCode根据请求添加了一些XML。 – user7653815
<TextView
android:id="@+id/rotated_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:alpha=".3"
android:background="@drawable/grey_capsule"
android:gravity="center"
android:textColor="@android:color/white"
android:textSize="14sp"
android:padding="4dp"
android:layout_marginLeft="-50dp"
android:rotation="-90"
android:text="Andrew Coder" />
谢谢........... –
如果您不需要动画,那么您可以尝试类似question中提供的解决方案。文字将自下而上写入。
它支持所有基本的TextView特点:
- 文字大小
- 文本颜色
- 文本字体
- 文本填充
- 通过XML的使用
要点是覆写onDraw()
和onMeasure()
种方法基于文本的参数:
@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);
canvas.rotate(-90);
canvas.drawText(this._text, 0, 0, this._textPaint);
}
}
@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));
}
}
请看看Vertical (rotated) label in Android看到完整的源代码。
- 1. 直接显示相机旋转90度
- 2. 如何旋转TeeChart 90度
- 3. 如何旋转StageVideo 90度
- 4. 旋转CALayer 90度?
- 5. 旋转台90度
- 6. CGAffineTransform旋转90度
- 7. 旋转位图90度并保存
- 8. 如何顺时针旋转90度(swift3)
- 9. 如何旋转线形90度?
- 10. 如何将输出旋转90度?
- 11. 如何旋转90度的UIImage?
- 12. 了解如何旋转矩阵90度
- 13. 图片90度旋转AVCam
- 14. C#旋转位图90度
- 15. 地图旋转了90度?
- 16. 旋转JLabel 90度爪哇
- 17. 旋转(90度)的ViewGroup根
- 18. 圆球90度旋转
- 19. 旋转NSImageView的CALayer 90度
- 20. 矩形旋转90度libgdx
- 21. 旋转GoJs形状90度
- 22. 子报表旋转90度
- 23. 如何旋转TextView?
- 24. 如何让旋转动画每次点击旋转90度?
- 25. 如何旋转TextView
- 26. 旋转画布顺时针旋转90度,并更新宽度高度
- 27. 在IE和Firefox中垂直显示文本(旋转90度)
- 28. 加载并显示图像:为什么它旋转90°?
- 29. 人力车图旋转刻度-90度
- 30. 旋转图像,并显示旋转角度
http://stackoverflow.com/questions/1258275/vertical-rotated-label-in-android检查了这个并解决了我的问题 – sakshi
从API 11(Android 3.0)开始,可以在XML中执行此操作。 http://stackoverflow.com/questions/3774770/sideways-view-with-xml-android – chobok