问题
Im trying to rotate a textview but when i rotate it, it keeps the width and height information
i want to the selected area gone i just want to text appears on the green indicator aligned centered. i'm changing the text 'Small Text' in java part but when the length of the text changes the alignment goes crazy (picture3)
here is my code what am i doing wrong ?
<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/ -> 2 http://postimg.org/image/lfdj7udhv/ -> 3
回答1:
You're probably best served by writing a simple custom control that inherits from TextView (via nidhi)
Here's a copy of the class.
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);
}
}
There are some drawbacks to this solution (mentioned in the blog post), but it works pretty well. This is the simplest and most isolated solution I've found. It can be dropped into a code file and immediately put to work. Plus, it shows up correctly in the layout preview, unlike most hacks involving rotation animations.
来源:https://stackoverflow.com/questions/23311484/textview-rotation-keeps-width-and-height