不使用背景图片的Android布局形状?

时间:2015-06-16 05:31:14

标签: android android-layout

enter image description here

在我不想使用背景图像的情况下,如何使布局看起来像跟随快照。

4 个答案:

答案 0 :(得分:1)

创建单个图像的九个补丁图像。以下链接有助于您生成九个补丁图像。

https://romannurik.github.io/AndroidAssetStudio/nine-patches.html

答案 1 :(得分:0)

您可以像这个三角形一样创建一个ImageView并将其放在您需要的位置。

看看如何使用xml创建三角形:Android triangle (arrow) defined as an XML shape

甚至可以创建TextView:

<TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="▼"/>

并把它放在原处。

三角形的Unicode:link

答案 2 :(得分:0)

您还可以绘制自定义视图:

public class MyBackground  extends View{
    private Path path = new Path();
    private Paint paint = new Paint();


    public MyBackground(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyBackground(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);
    }

    public MyBackground(Context context) {
        this(context, null);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.GRAY);
        int triangleSide = getWidth() / 10; // triangle is 1/10-th of side
        int offsetFromRightSide = triangleSide;
        int startX = getWidth() - triangleSide - offsetFromRightSide;

        path.reset();
        path.moveTo(startX, 0);
        path.lineTo(startX + triangleSide/2, triangleSide/2);
        path.lineTo(startX + triangleSide, 0);

        canvas.drawPath(path, paint);
    }
}

并像这样使用

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    tools:context=".MainActivity">
    <your.package.name.MyBackground
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

或者还有一个“代码方式”。创建自定义背景Drawable

public class Background extends Drawable {
    private Path path = new Path();
    private Paint paint = new Paint();

     public Background(){
         paint.setColor(Color.WHITE);
         paint.setStyle(Paint.Style.FILL);
         paint.setAntiAlias(true);
     }

    @Override
    public void draw(Canvas canvas) {

        int triangleSide = getBounds().width() / 10; // triangle is 1/10-th of side
        int offsetFromRightSide = triangleSide;
        int startX = getBounds().width() - triangleSide - offsetFromRightSide;

        path.reset();
        path.moveTo(startX, 0);
        path.lineTo(startX + triangleSide/2, triangleSide/2);
        path.lineTo(startX + triangleSide, 0);

        canvas.drawColor(Color.GRAY);
        canvas.drawPath(path, paint);
    }

    @Override public void setAlpha(int alpha) {}
    @Override public void setColorFilter(ColorFilter cf) {}
    @Override public int getOpacity() {return 0;}
}

并像这样使用它:

RelativeLayout layout = (RelativeLayout) findViewById(R.id.root);
layout.setBackgroundDrawable(new Background());

答案 3 :(得分:0)

在相对布局中使用以下代码。希望它可以帮到你。

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableRight="@drawable/triangleImage"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"/>