在静态布局视图上实现OnDraw方法

时间:2015-02-06 17:43:00

标签: java android

我想为我在Android Studio中自定义的布局实现onDraw()方法。我知道如何使用自定义视图执行此操作,但我希望使用自定义视图执行此操作,并且无法在Internet上获取有关此信息。 这是布局代码:

public class MainActivity extends ActionBarActivity
{
    CustomDrawableView mCustomDrawableView;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        mCustomDrawableView = new CustomDrawableView(this);

        //layoutMain.addView(mCustomDrawableView);
        //setContentView(mCustomDrawableView);
        //mCustomDrawableView.requestFocus();
    }
}

自定义可绘制视图:

class CustomDrawableView extends View
{
    private ShapeDrawable mDrawable;

    public CustomDrawableView(Context context)
    {
        super(context);

        int x = 10;
        int y = 10;
        int width = 300;
        int height = 50;

        mDrawable = new ShapeDrawable(new OvalShape());
        mDrawable.getPaint().setColor(0xff74AC23);
        mDrawable.setBounds(x, y, x + width, y + height);
    }

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

activity_main.xml:

<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:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                tools:context=".MainActivity">

    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_below="@+id/textView"
        android:layout_toEndOf="@+id/textView"
        android:layout_marginTop="82dp"/>

</RelativeLayout>

问题是我只能选择一个视图:静态没有OnDraw()或动态有OnDraw()但没有我的所有按钮/滑块和其他元素。我可以将它们组合起来并实现主要的OnDraw()吗?

        02-06 19:33:49.793    1823-1823/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cepp.myapplication2/com.example.cepp.myapplication2.MainActivity}: android.view.InflateException: Binary XML file line #26: Error inflating class com.example.cepp.myapplication2.CustomDrawableView
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
                at android.app.ActivityThread.access$600(ActivityThread.java:141)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
                at android.os.Handler.dispatchMessage(Handler.java:99)
                at android.os.Looper.loop(Looper.java:137)
                at android.app.ActivityThread.main(ActivityThread.java:5041)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:511)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
                at dalvik.system.NativeStart.main(Native Method)
      Caused by: android.view.InflateException: Binary XML file line #26: Error inflating class com.example.cepp.myapplication2.CustomDrawableView
                at android.view.LayoutInflater.createView(LayoutInflater.java:596)
                at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
                at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
                at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
                at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
                at com.example.cepp.myapplication2.MainActivity.onCreate(MainActivity.java:98)
                at android.app.Activity.performCreate(Activity.java:5104)
                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5041)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)
      Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
                at java.lang.Class.getConstructorOrMethod(Class.java:460)
                at java.lang.Class.getConstructor(Class.java:431)
                at android.view.LayoutInflater.createView(LayoutInflater.java:561)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
            at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
            at com.example.cepp.myapplication2.MainActivity.onCreate(MainActivity.java:98)
            at android.app.Activity.performCreate(Activity.java:5104)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5041)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

您永远不会将视图放在活动中。像对待任何其他视图一样对待自定义视图并将其放入布局

activity_main.xml中

<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:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            android:paddingBottom="@dimen/activity_vertical_margin"
            tools:context=".MainActivity">

    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        <!--Add this line or the custom view will push these off the screen-->
        android:layout_parentTop="true"
        <!--  ************************ -->
        android:id="@+id/textView"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_below="@+id/textView"
        android:layout_toEndOf="@+id/textView"
        android:layout_marginTop="82dp"/>

    <com.you.yourapp.CustomDrawableView  <!--com.you.yourapp is a stand in for the package name for your apk-->
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="+@id/button" />

</RelativeLayout>

然后它会表现得更像或更像ImageView

编辑2: 该模式通常是View代码中的错误。查看Priview(当您打开xml文件时,在Android Studio中一直到右侧)。如果视图正常,它将显示在预览中。如果它不会告诉你例外的位置。