It is possible to create this simple shape in Android XML?

I am trying to create the following simple shape in Android XML:

Just a rectangle combined with a triangle to form an arrow. The shape should be used as navigation button background.

On Windows Phone it would be no problem to create this shape in XAML but the XML drawing capabilities of Android seem to be much more limited.

I tried to create a layer-list drawable using a normal rectangle as "body" and rotated rectangle as triangle. Creating a triangle by rotating a rectangle works fine as long as the triangle is aligned with the border. As soon as I try to move/translate the triangle to the left (to the end of the body/box), the triangle is not triangle any more but of course just a rotated rectangle... Is there any way to clip off the one half of the rotated rectangle and move it to the end of the box in XML?

Of course this can be done in Code/Java, but I would like to know if it also possible XML.

I would need this kind of button in many different sizes and using a XML drawable would be much better (from my point of view) than using dozens of PNGs.


试试这个自定义形状:

Shape shape = new Shape() {
    Path path = new Path();
    @Override
    protected void onResize(float width, float height) {
        path.reset();
        path.lineTo(width - height / 2, 0);
        path.lineTo(width, height / 2);
        path.lineTo(width - height / 2, height);
        path.lineTo(0, height);
        path.close();
    }
    @Override
    public void draw(Canvas canvas, Paint paint) {
        canvas.drawPath(path, paint);
    }
};
ShapeDrawable d = new ShapeDrawable(shape);
d.getPaint().setColor(0xff6699bb);

someView.setBackgroundDrawable(d);

I can't comment so will answer.. I like the layerlist idea. example here:

Android custom shape

and triange example here: http://looksok.wordpress.com/2013/08/24/android-triangle-arrow-defined-as-an-xml-shape/

combining those answers may work.

链接地址: http://www.djcxy.com/p/20954.html

上一篇: 定期为R闪光输出捕获猫的输出(renderPrint)

下一篇: 在Android XML中创建这个简单的形状是可能的吗?