在Android开发过程中,有时候会有斜着的标签,可用TextView实现。
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://www.baidu.com/"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<com.clins.RotateTextView
android:layout_width="50dp"
android:layout_alignParentRight="true"
android:gravity="center_horizontal"
android:text="热门"
android:textSize="12sp"
custom:rotate="45"
android:layout_height="50dp" />
</RelativeLayout>
重写TextView:
package com.clins;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.TextView;
/**
* @author cclin
* @date 2015/5/8
*/
public class RotateTextView extends TextView {
private static final String NAME_SPACE = "http://www.baidu.com/";
private static final String ATTR_ROTATE = "rotate";
private static final int DEFAULTVALUE_DEGREES = 0;
private int degrees;
public RotateTextView(Context context, AttributeSet attrs) {
super(context, attrs);
degrees = attrs.getAttributeIntValue(NAME_SPACE, ATTR_ROTATE, DEFAULTVALUE_DEGREES);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.rotate(degrees, getMeasuredWidth() / 2f, getMeasuredHeight() / 2f);
super.onDraw(canvas);
}
}