安卓:请用三角形使用XML定义按钮(绘制) [英] Android: Make a button with triangle shape using xml definitions (drawable)

查看:3365
本文介绍了安卓:请用三角形使用XML定义按钮(绘制)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过使用XML definiton创建此使用按钮(TextView中):

在活动的布局,我有:

 <的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:背景=@可绘制/ button_arrow<  - 我需要实现这个 - >
        机器人:可点击=真
        机器人:drawablePadding =7DP
        机器人:重力=中心
        机器人:drawableLeft =@可绘制/ music_cloud
        机器人:的onClick =exportSong
        机器人:填充=20dp
        机器人:文本=@字符串/ export_upload
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium
        机器人:文字颜色=@色/ dark_yellow_text_color
        机器人:TEXTSTYLE =黑体/>
 

我创办了几个职位:

<一个href="http://stackoverflow.com/questions/2517589/making-a-triangle-shape-using-xml-definitions">making-a-triangle-shape-using-xml-definitions

Android的三角形(箭头)被定义为一个XML图形

Android的 - 让使用XML

我试图修改多个XML定义但没有什么是好的。有一些简单的方法如何实现这种形状?此外,它应该有一个透明的背景。

解决方案

如果有人仍然有问题,这样的:

  1. XML:

     &LT;项目的android:顶部=45dp&GT;
        &LT;形状&GT;
            &LT;大小机器人:身高=100dp机器人:宽=90dp/&GT;
            [固体机器人:颜色=@机器人:彩色/ holo_orange_light/&GT;
        &LT; /形状&GT;
    &LT; /项目&GT;
    &LT;项目的android:顶部=36dp机器人:左边=11DP&GT;
        &LT;旋转
            机器人:fromDegrees =45
            机器人:toDegrees =0
            机器人:pivotX =80%
            机器人:pivotY =20%&GT;
            &LT;形状&GT;
                &LT;大小机器人:宽=40dp
                    机器人:身高=30dp/&GT;
                &LT;行程机器人:颜色=@机器人:彩色/ holo_orange_light机器人:宽=1DP/&GT;
                [固体机器人:颜色=@机器人:彩色/ holo_orange_light/&GT;
            &LT; /形状&GT;
        &LT; /旋转&GT;
    &LT; /项目&GT;
    &LT; /层列表&gt;
     

  2. TextView的覆盖,并用它在你的布局:

     公共类CustomTextView扩展TextView的{
    
        私人诠释mWidth;
        私人诠释mHeight;
    
    
        公共CustomTextView(上下文的背景下,ATTRS的AttributeSet){
            超(背景下,ATTRS);
    
        }
    
        @覆盖
        保护无效的OnDraw(帆布油画){
    
           super.onDraw(画布);
            油漆mPaint =新的油漆();
            。INT颜色= getResources()的getColor(R.color.YourColor);
    
            mPaint.setColor(颜色);
            路径的mpath =新路径();
            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);
    
    
        }
    }
     

关于XML例子:有两个矩形overlapping.You必须与价值观玩了很多,这使得它很难在不同的视图使用。我认为使用自定义视图是在这种情况下,最好的解决办法。

I want create this using button (TextView) by using XML definiton:

In layout of the Activity I have:

    <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" />

I founded several posts:

making-a-triangle-shape-using-xml-definitions

Android triangle (arrow) defined as an XML shape

or Android - make an arrow shape with xml

I tried modify several XML definition but nothing was good. Is there some easy way how to implement this shape? Also it should have a transparent background.

解决方案

If someone still has issues with this :

  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>
    

  2. override TextView and use it in your layout:

    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);
    
    
        }
    }
    

Regarding the xml example: there are two rectangles overlapping.You have to play around with the values a lot and this makes it difficult to use on different views. I think using a custom view is the best solution in this case.

这篇关于安卓:请用三角形使用XML定义按钮(绘制)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆