使用Android自定义视图绘制边框

发布于 2024-09-25 04:07:20 字数 2427 浏览 5 评论 0原文

我试图通过绘制自定义视图来绘制自定义边框。以下是边界一侧的示例:

package com.sparkydev.guessaphrase;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.util.AttributeSet;
import android.view.View;

public class LeftBorder extends View {
    private ShapeDrawable vertRect, horizRect;

    public LeftBorder(Context context, AttributeSet attributeset) {
        super(context, attributeset);

        int width = this.getWidth();
        int height = this.getHeight();

        vertRect = new ShapeDrawable(new RectShape());
            vertRect.getPaint().setColor(Color.RED);
            vertRect.setBounds(0, 0, width/10, height);
        horizRect = new ShapeDrawable(new RectShape());
            horizRect.getPaint().setColor(Color.RED);
            horizRect.setBounds(0, 0, width, height/9);

    }

    protected void onDraw(Canvas canvas){
        vertRect.draw(canvas);
        horizRect.draw(canvas);
    }

}

另一侧的定义方式几乎相同。 XML 的定义如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@drawable/wallpaper">


    <!-- Declares left border's position, which is
    drawn programmatically.-->
    <com.sparkydev.guessaphrase.LeftBorder
        android:id="@+id/leftborder"
        android:layout_alignParentLeft="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />



        <!-- Used a FrameLayout to contain
        the menu buttons.-->
        <FrameLayout 
        android:id="@+id/FrameLayout01" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        >

            <!-- Menu buttons -->

        </FrameLayout>


    <!-- Declares right border position (on right
    of screen, below the left border) -->
    <com.sparkydev.guessaphrase.RightBorder
        android:layout_alignParentRight="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/leftborder"
    />



</RelativeLayout>

问题是,边框根本不显示。显示背景,但没有其他内容。

I'm trying to draw a custom border by drawing a custom view. Here is a sample of one side of the border:

package com.sparkydev.guessaphrase;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.util.AttributeSet;
import android.view.View;

public class LeftBorder extends View {
    private ShapeDrawable vertRect, horizRect;

    public LeftBorder(Context context, AttributeSet attributeset) {
        super(context, attributeset);

        int width = this.getWidth();
        int height = this.getHeight();

        vertRect = new ShapeDrawable(new RectShape());
            vertRect.getPaint().setColor(Color.RED);
            vertRect.setBounds(0, 0, width/10, height);
        horizRect = new ShapeDrawable(new RectShape());
            horizRect.getPaint().setColor(Color.RED);
            horizRect.setBounds(0, 0, width, height/9);

    }

    protected void onDraw(Canvas canvas){
        vertRect.draw(canvas);
        horizRect.draw(canvas);
    }

}

And the other side is defined pretty much the same way. The XML is defined as such:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@drawable/wallpaper">


    <!-- Declares left border's position, which is
    drawn programmatically.-->
    <com.sparkydev.guessaphrase.LeftBorder
        android:id="@+id/leftborder"
        android:layout_alignParentLeft="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />



        <!-- Used a FrameLayout to contain
        the menu buttons.-->
        <FrameLayout 
        android:id="@+id/FrameLayout01" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        >

            <!-- Menu buttons -->

        </FrameLayout>


    <!-- Declares right border position (on right
    of screen, below the left border) -->
    <com.sparkydev.guessaphrase.RightBorder
        android:layout_alignParentRight="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/leftborder"
    />



</RelativeLayout>

The problem is, the borders are not showing up at all. The background shows, but nothing else.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你是暖光i 2024-10-02 04:07:20

来自“构建自定义组件”开发指南,当创建自定义视图时,除非您另行指定,否则它将具有 100x100 的设置大小。看看这是否与您遇到的问题有关。

“如果您希望组件显示某些内容,您几乎肯定会想要重写 onMeasure() 并且也可能需要重写 onDraw()。虽然两者都具有默认行为,默认的 onDraw() 将不执行任何操作,并且默认的 onMeasure() 将始终设置 100x100 的大小 — 这可能不是您想要的。”

From the "Building Custom Components" dev guide, when creating a custom view, it will have a set size of 100x100 until you specify otherwise. See if this has anything to do with the issue you are seeing.

"You will almost certainly want to override onMeasure() and are also likely to need to override onDraw() if you want the component to show something. While both have default behavior, the default onDraw() will do nothing, and the default onMeasure() will always set a size of 100x100 — which is probably not what you want."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文