如何创建一个简单的自定义视图? [英] How to create a simple custom View?

查看:111
本文介绍了如何创建一个简单的自定义视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建查看 Android上的定制。我曾尝试做尽可能的简单,创造一个几乎空类 MyView的在用它我的LinearLayout 但申请失败的开始强制关闭。我怎样才能做一个简单的自定义查看?据构建自定义组件,从而查看获取尺寸100×100,如果我不覆盖 onMeasure()

 公共类MyView的扩展视图{

    公共MyView的(上下文的背景下){
        超(上下文);
    }
}
 

和我在使用它的LinearLayout

 <查看
    类=com.example.MyView
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_weight =0.0/>
 

我是什么做错了吗?


如果我用的是构造itemon建议和超相应的呼叫。那么强制关闭走了,但我的的LinearLayout 坏了,以后 MyView的的组件将不会显示。

下面是我的的main.xml

 < XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直
    >
<的TextView
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_weight =0.0
    机器人:后台=#F00
    机器人:文本=你好
/>
<查看
    类=com.example.MyView
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_weight =0.0
/>
<的TextView
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_weight =0.0
    机器人:后台=#00F
    机器人:文字=世界
/>
< / LinearLayout中>
 

解决方案

可能是,你可以这样定义另一个构造方法:

 公共MyView的(上下文的背景下,ATTRS的AttributeSet)
 

Android框架将尝试从上面的构造你的看法构建用户界面。

I would like to create an custom View on Android. I have tried to do it as simple as possible and created an almost empty class MyView and used it in my LinearLayout but the application fails on start with "Force Close". How can I do a simple custom View? According to Building Custom Components the View gets the size 100x100 if I don't override onMeasure().

public class MyView extends View {

    public MyView(Context context) {
        super(context);
    }
}

And I use it in a LinearLayout with:

<view
    class="com.example.MyView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.0" />

What am I doing wrong?


If I use the constructor that itemon suggest and the corresponding call to the superclass. Then the "Force Close" is gone, but my LinearLayout is broken, the components after MyView isn't shown.

Here is my main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.0"
    android:background="#f00"
    android:text="Hello"
/>
<view
    class="com.example.MyView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.0"
/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.0"
    android:background="#00f"
    android:text="World"
/>
</LinearLayout>

解决方案

may be you could define another constructor method like this:

public MyView(Context context, AttributeSet attrs)

the android framework will try to build the UI with your view from the constructor above.

这篇关于如何创建一个简单的自定义视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆