问题
ive found a lot of info on how to subclass a textview & display the text vertically.
Iam trying to display a star next to the text and iam having problems. maybe you have some ideas on this.
Subclass:
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 )
{
TextPaint textPaint = getPaint();
textPaint.setColor( getCurrentTextColor() );
textPaint.drawableState = getDrawableState();
canvas.save();
if ( topDown )
{
canvas.translate( getWidth(), 0 );
canvas.rotate( 90 );
}
else
{
canvas.translate( 0, getHeight() );
canvas.rotate( -90 );
}
canvas.translate( getCompoundPaddingLeft(), getExtendedPaddingTop() );
getLayout().draw( canvas );
canvas.restore();
}
}
and Iam setting the star with this code from my fragment:
ItemTextView.setCompoundDrawablesWithIntrinsicBounds(0, 0, android.R.drawable.btn_star_big_on, 0);
no star is displayed.only blank space. and ideas???
the subclass is used in a listview. under a specific condition one item of the listview I want to have a star next to it, so **I think i have to do it by code.
回答1:
you can use android:drawableLeft
attribute in your xml layout.
http://developer.android.com/reference/android/widget/TextView.html#attr_android:drawableLeft
回答2:
Try to use a TableLayout in your layout xml, and then play with its rows, it's easier than doing it by code.
来源:https://stackoverflow.com/questions/11435170/android-vertical-textview-with-image-next-to-it