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

不使用背景图像的Android布局形状?,android,android-layout,Android,Android Layout,在我不想使用背景图像的情况下,如何使布局看起来像下面的快照。您可以创建一个类似三角形的图像视图,并将其放置在需要的位置 看看如何使用xml创建三角形: 甚至您也可以创建文本视图: <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="▼"/> 把它放在那个地方 三角形的Unicode:创建单个图像的九块图像。以下链


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

您可以创建一个类似三角形的图像视图,并将其放置在需要的位置

看看如何使用xml创建三角形:

甚至您也可以创建文本视图:

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

把它放在那个地方


三角形的Unicode:

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


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

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>
然后像这样使用它:

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

在相对布局中使用以下代码。希望对你有帮助

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


这似乎是一个很好的方法。还有其他方法吗?这将使三角形位于文本视图的左侧,垂直居中。这不是OP所要求的。
    <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"/>