【问题标题】:android: OnDraw in a xml layoutandroid: xml布局中的OnDraw
【发布时间】:2014-06-22 01:35:01
【问题描述】:

MainActivity .java 导入android.app.Activity; 导入android.os.Bundle; 导入android.view.Window;

public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(new GView(this);
}
}

start.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"
tools:context="${packageName}.${activityClass}" >

<RelativeLayout
    android:id="@+id/screen"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginBottom="140dp" >

</RelativeLayout>

GView.java 包 com.example.nep;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.View;

public class GView extends View
{
private Bitmap bmp;
Hikari hikari;

public GView(Context context)
{
    super(context);
    bmp = BitmapFactory.decodeResource(getResources(), R.drawable.bit);
}

public void draw(Canvas canvas) {
    canvas.drawColor(Color.WHITE);
    canvas.drawBitmap(bmp, 10 , 10, null);
}
}

上面是我的代码,我想要做的是删除 setContentView(new GView(this); 并用 setContentView(R.layout.start); 替换它,但我希望 GView 在我的第二个布局中,这个

    <RelativeLayout
    android:id="@+id/screen"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginBottom="140dp" >

要像gameboy那样设置,上面的屏幕和下面的按钮,我该怎么做?

【问题讨论】:

    标签: java android xml layout


    【解决方案1】:

    我猜你想在你的 xml 布局中使用 Gview,就像 imageview 等其他视图一样。您在 eclipse 的布局编辑器中有一个名为 custom 的字段。您可以像任何其他视图一样拖放。如果您想以编程方式编写代码,那么这应该可以工作:

    <com.mypackage.name.GView
    android:id="@+id/gview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     >
    

    在这样设置之后,你的问题就得到了回答:只需使用:

    setContentView(R.layout.main)j;//remove new Gview()
    

    【讨论】:

    • 我照你说的做了,但我得到一个错误:自定义视图 GView 没有使用 2 或 3 参数视图构造函数; XML 属性将不起作用
    猜你喜欢
    • 2012-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-29
    • 2014-07-17
    • 1970-01-01
    相关资源
    最近更新 更多