1.TextView的mes属性:
(不建议使用)
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:maxEms="1" android:textSize="28sp" android:textColor="@color/black" android:text="竖排版"/>TextView的width一定不能是match_parent
2.PlumbTextViwe
(github开源地址:https://github.com/lybeat/PlumbTextView)
Gradle依赖:compile 'cc.sayaki.widget:plumb-textview:1.0.1'
Layout使用:
<cc.sayaki.widget.PlumbTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingBottom="30dp" android:paddingTop="30dp" sayaki:columnSpacing="20dp" sayaki:leftLine="true" sayaki:leftLineColor="@color/colorAccent" sayaki:leftLinePadding="4dp" sayaki:letterSpacing="6dp" sayaki:regex="[,。?!]" sayaki:text="@string/text" sayaki:textColor="@color/colorAccent" sayaki:textSize="16sp" sayaki:textStyle="bold|italic" />
3.自定义TextView
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 boolean setFrame(int l, int t, int r, int b){ return super.setFrame(l, t, l+(b-t), t+(r-l)); } @Override public void draw(Canvas canvas){ if(topDown){ canvas.translate(getHeight(), 0); canvas.rotate(90); }else { canvas.translate(0, getWidth()); canvas.rotate(-90); } canvas.clipRect(0, 0, getWidth(), getHeight(), android.graphics.Region.Op.REPLACE); super.draw(canvas); } }