保持文本在菱形背景内

时间:2016-04-01 11:51:59

标签: android android-layout

我在这里画一个空白!

我有一个菱形背景的textview。该文本需要包含在钻石中。 像这样。

textview_with_diamond_shaped_bg

我该怎么做?有没有办法在textview中定义文本的边界?

1 个答案:

答案 0 :(得分:1)

修改

将以下代码放在像DiamondTextView这样的java类中:

public class DiamondTextView extends TextView{

    public DiamondTextView(Context ctx, AttributeSet attrs) {
        super(ctx, attrs);
    }

    Paint mBorderPaint = new Paint();

        @Override
        protected void onDraw(Canvas canvas) {

            mPath.moveTo(mWidth/2 , 0);
            mPath.lineTo(mWidth , mHeight/2);
            mPath.lineTo(mWidth /2 , mHeight);
            mPath.lineTo(0 , mHeight/2);
            mPath.lineTo( mWidth/2 ,0);

            //setup the paint for fill
            mBorderPaint.setAlpha(255);
            mBorderPaint.setColor(mBorderColor);
            mBorderPaint.setStyle(Paint.Style.FILL);
            borderPaint.setStrokeWidth(mBorderWidth);

            //draw it
            canvas.drawPath(mPath ,mBorderPaint);

            //setup the paint for stroke
            mBorderPaint.setAlpha(51);
            mBorderPaint.setStyle(Paint.Style.STROKE);

            //draw it again
            canvas.drawPath(mPath ,mBorderPaint);

            super.onDraw(canvas);
        }
}

并将其称为您要使用此textView的地方:

 <com.erginus.ABC.Commons.DiamondTextView...../>

有关详细信息,请查看此处:http://developer.android.com/reference/android/graphics/drawable/shapes/PathShape.html

您也可以使用以下库:https://github.com/siyamed/android-shape-imageview

相关问题