Pergunta

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

Foi útil?

Solução

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.

Outras dicas

I used a navigate margin to remove the extra space.

<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"/>

Put the TextView in a FrameLayout

    <FrameLayout
      android:id="@+id/frameLayoutMarqueeText"
      android:layout_width="match_parent"
      android:layout_height="match_parent">

        <TextView
          android:id="@+id/textViewMarquee"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="the difference between tomorrow and yesterday is that tomorrow is the day after the present day while yesterday is the day before today it can also be understood as a day ago." />
    </FrameLayout>

And then set the width, height of FrameLayout to the height, width of the parent or screen. (Interchange each other).

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
frameLayoutMarqueeText.setLayoutParams(new LinearLayout.LayoutParams(height, width));
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top