Android:使用xml定义创建一个三角形的按钮(可绘制)

Pep*_*tal 10 xml android textview android-button android-drawable

我想通过使用XML定义使用按钮(TextView)创建它:

我的形象

在我的活动布局中:

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button_arrow" <!-- I NEED IMPLEMENT THIS -->
        android:clickable="true"
        android:drawablePadding="7dp"
        android:gravity="center"
        android:drawableLeft="@drawable/music_cloud"
        android:onClick="exportSong"
        android:padding="20dp"
        android:text="@string/export_upload"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/dark_yellow_text_color"
        android:textStyle="bold" />
Run Code Online (Sandbox Code Playgroud)

我创建了几个帖子:

制作-A-三角形形状使用-XML的定义

Android三角形(箭头)定义为XML形状

Android - 使用xml制作箭头形状

我尝试修改几个XML定义,但没有什么是好的.如何实现这种形状有一些简单的方法吗?它也应该有一个透明的背景.

Han*_*eck 8

如果有人仍然有这个问题:

  1. XML:

    <item android:top="45dp">
        <shape>
            <size android:height="100dp" android:width="90dp"/>
            <solid android:color="@android:color/holo_orange_light" />
        </shape>
    </item>
    <item android:top="36dp" android:left="11dp">
        <rotate
            android:fromDegrees="45"
            android:toDegrees="0"
            android:pivotX="80%"
            android:pivotY="20%" >
            <shape>
                <size android:width="40dp"
                    android:height="30dp"/>
                <stroke android:color="@android:color/holo_orange_light" android:width="1dp"/>
                <solid android:color="@android:color/holo_orange_light" />
            </shape>
        </rotate>
    </item>
    </layer-list>
    
    Run Code Online (Sandbox Code Playgroud)
  2. 覆盖TextView并在布局中使用它:

    public class CustomTextView extends TextView {
    
        private int mWidth;
        private int mHeight;
    
    
        public CustomTextView(Context context, AttributeSet attrs)  {
            super(context, attrs);
    
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
    
           super.onDraw(canvas);
            Paint mPaint = new Paint();
            int color = getResources().getColor(R.color.YourColor);
    
            mPaint.setColor(color);
            Path mPath = new Path();
            mPath.moveTo(.0f, this.getHeight());
            mPath.lineTo(0.8f * this.getWidth(), this.getHeight());
            mPath.lineTo(this.getWidth(), 0.5f * this.getHeight());
            mPath.lineTo(0.8f * this.getWidth(), .0f);
            mPath.lineTo(.0f, .0f);
            mPath.lineTo(.0f, this.getHeight());
    
            canvas.clipPath(mPath);
            canvas.drawPath(mPath,mPaint);
    
    
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

关于xml示例:有两个矩形重叠.您必须大量使用这些值,这使得难以在不同的视图上使用.我认为在这种情况下使用自定义视图是最佳解决方案.