TextView旋转保持宽度和高度

时间:2014-04-26 13:21:04

标签: android xml rotation textview alignment

我试图旋转文本视图但是当我旋转它时,它会保留宽度和高度信息

我希望所选区域消失,我只想将文字显示在绿色指示器对中居中。我正在更改java部分中的文本'Small Text',但是当文本的长度发生变化时,对齐变得疯狂(图3)

这是我的代码我做错了什么?

<ImageView
        android:id="@+id/statusIcon"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:src="@drawable/bulletgreen"
        android:layout_marginRight="5dp"
         />

        <TextView
            android:id="@+id/status"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_marginLeft="5dp"
            android:rotation="-90"
            android:text="Small Text"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textSize="10sp"
            android:textStyle="bold" />

http://postimg.org/image/9e92i8k2j/ - &gt; 2 http://postimg.org/image/lfdj7udhv/ - &gt; 3

2 个答案:

答案 0 :(得分:8)

您可能最好通过编写一个从TextView继承的简单自定义控件(通过nidhi)来获得最佳服务

这是该课程的副本。

public class VerticalTextView extends TextView {
final boolean topDown;

public VerticalTextView(Context context,
                        AttributeSet attrs) {
    super(context, attrs);
    final int gravity = getGravity();
    if (Gravity.isVertical(gravity) && (gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) {
        setGravity( (gravity & Gravity.HORIZONTAL_GRAVITY_MASK) | Gravity.TOP );
        topDown = false;
    } else {
        topDown = true;
    }
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(heightMeasureSpec, widthMeasureSpec);
    setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
}

@Override
protected void onDraw(Canvas canvas) {
    if (topDown) {
        canvas.translate(getWidth(), 0);
        canvas.rotate(90);
    } else {
        canvas.translate(0, getHeight());
        canvas.rotate(-90);
    }

    canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop());
    getLayout().draw(canvas);
}
}

此解决方案存在一些缺点(在博客文章中提到),但它的效果非常好。这是我发现的最简单,最孤立的解决方案。它可以放入代码文件中并立即投入使用。此外,它在布局预览中正确显示,与大多数涉及旋转动画的黑客不同。

答案 1 :(得分:0)

我使用导航边距删除了多余的空间。

<TextView
    android:rotation="-90"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginLeft="-10dp"
    android:layout_marginRight="-10dp"/>
相关问题