android - 使用自定义 View 绘制边框 Android

标签 android custom-view

我正在尝试通过绘制自定义 View 来绘制自定义边框。这是边界一侧的示例:

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>

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

最佳答案

来自 the "Building Custom Components" dev guide ,在创建自定义 View 时,它将具有 100x100 的设置大小,除非您另外指定。查看这是否与您遇到的问题有关。

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

关于android - 使用自定义 View 绘制边框 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3799887/

相关文章:

android - 在 Android 中向自定义 View 添加按钮

swift - 如何在按下位于自定义 UIView 内且带有 XCode Storyboard的 UIButton 时创建 Segue

java - 如何在 android 2.2.2 (Froyo) 中使用 BitmapRegionDecoder 代码?

java - 线程完成时更改布局

java - 如何停止android中默认出现软键盘

android - 根据点击的 TextView 将数据从应用程序小部件传递到 Activity

java - 如何在Activity中添加 float View

android - 在我的 Activity 中放置带有 sin/cos 波的图表

xcode - 如何以编程方式更改自定义 View 对象的成员变量?

cocoa 抽吸噪音