【问题标题】:How make a banner or ribbon with textview or any layout [closed]如何使用 textview 或任何布局制作横幅或功能区 [关闭]
【发布时间】:2016-01-04 18:36:46
【问题描述】:

嘿,我想在 android 中使用 xml 或 java 制作与 textview 或任何布局 类似的 横幅或功能区。我无法弄清楚如何做到这一点。
所以请帮我解决它真正需要的问题。

【问题讨论】:

  • 这是一个带有背景 Drawable 的 TextView...

标签: java android xml textview banner


【解决方案1】:

要创建这样的横幅,您需要做这些事情

  1. 通过调整this code 创建倒直角三角形的Shape drawable
  2. 将 textView 的背景设置为此形状
  3. 将 textView Gravity 更改为右/端对齐

【讨论】:

    【解决方案2】:

    这是一个简单的图像视图,角落里有我自己写的文字横幅。相同的结构可以应用于任何类型的视图。

    public class BannerImageView extends ImageView {
    
        private Paint mRibbonPaint;
        private Paint mTextPaint;
        private Paint mBoxPaint;
        private float mScale;
        private String mBannerText;
    
        public BannerImageView(Context context, AttributeSet attrs) {
            super(context, attrs);
            initPainters(attrs);
        }
    
        public BannerImageView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            initPainters(attrs);
        }
    
        private void initPainters(AttributeSet attrs) {
            TypedArray attributes = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.BannerImageView, defStyleAttr, 0);
            mBannerText = attributes.getString(R.styleable.BannerImageView_label);
            mBoxPaint = new Paint();
            int white = ContextCompat.getColor(getContext(), R.color.white);
            mBoxPaint.setColor(white);
            mBoxPaint.setAlpha(156);
            mRibbonPaint = new Paint();
            mRibbonPaint.setColor(ContextCompat.getColor(getContext(), R.color.banner_color));
            mRibbonPaint.setAntiAlias(true);
            mRibbonPaint.setStyle(Paint.Style.STROKE);
            mRibbonPaint.setStrokeCap(Paint.Cap.BUTT);
            mScale = getResources().getDisplayMetrics().density;
            mRibbonPaint.setStrokeWidth(dp(16));
            mTextPaint = new Paint();
            mTextPaint.setColor(white);
            mTextPaint.setTextSize( dp(12) );
        }
    
        /**
         * Converts dp to px
         * @param dp
         * @return
         */
        private float dp(float dp) {
            return dp * mScale + 0.5f;
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            if ( !TextUtils.isEmpty(mBannerText) ) {
                canvas.drawRect(0, 0, getRight(), getBottom(), mBoxPaint);
                canvas.drawLine(-dp(16), dp(64), dp(64), -dp(16), mRibbonPaint);
                canvas.save();
                canvas.rotate(-45, 0, 0);
                canvas.drawText(mBannerText, -dp(24), dp(38), mTextPaint);
                canvas.restore();
            }
        }
    

    然后在 values/attrs.xml 中声明要使用的额外属性

    <declare-styleable name="BannerImageView">
        <attr name="label" format="string"/>
    </declare-styleable>
    

    当然,您可以像往常一样在 XML 布局中使用,但请记住自定义属性的应用命名空间

    <com.my.package.BannerImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:label="Sold Out" />
    

    【讨论】:

    • 什么是defStyleAttr???
    • 它应该是您想要的属性的标识符(样式) - 这实际上对这个答案并不重要 - 请参阅此处stackoverflow.com/questions/6784854/…
    • 设置为零后也不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-14
    相关资源
    最近更新 更多