How to create a simple custom view?

I would like to create a custom View on Android. I tried to make it as simple as possible and created an almost empty MyView class and used it in my LinearLayout , but the application does not start with "Force Close". How can I make a simple custom View ? According to "Creating custom components", the View gets a size of 100x100 if I do not override onMeasure() .

 public class MyView extends View { public MyView(Context context) { super(context); } } 

And I use it in 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 suggested by itemon and the corresponding call to the superclass. Then "Force Close" disappeared, but my LinearLayout broken, components after MyView not displayed.

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> 
+7
source share
2 answers

Perhaps you could define another constructor method like this:

 public MyView(Context context, AttributeSet attrs) 

Android framework will try to create a user interface with your view from the constructor above.

+9
source

The Android Developers Guide has a section called โ€œCreating custom components.โ€ Unfortunately, the discussion of XML attributes only covers the declaration of the control inside the layout file, and not the processing of values โ€‹โ€‹inside the class initialization. These steps are as follows:

Declare attributes in \ attrs.xml values

 <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MyCustomView"> <attr name="android:text"/> <attr name="android:textColor"/> <attr name="extraInformation" format="string" /> </declare-styleable> </resources> 

Note the use of an unqualified name in the declare-styleable tag. Custom Android attributes, such as extraInformation, must be of the declaration type. Tags declared in a superclass will be available in subclasses without the need to update them.

Create Constructors

Since there are two constructors that use AttributeSet initialization, it is convenient to create a separate initialization method for the called constructors.

 private void init(AttributeSet attrs){ TypedArray a=getContext().obtainStyledAttributes(attrs,R.styleable.MyCustomView); //Use a Log.i("test",a.getString(R.styleable.MyCustomView_android_text)); Log.i("test",""+a.getColor(R.styleable.MyCustomView_android_textColor, Color.BLACK)); Log.i("test",a.getString(R.styleable.MyCustomView_android_extraInformation)); //Don't forget this a.recycle(); } 

R.styleable.MyCustomView is an autogenerated int [] resource, where each element is an attribute identifier. Attributes are generated for each property in XML, adding the attribute name to the element name. Attributes can then be retrieved from TypedArray using various get functions. If the attribute is not defined in XML, null is returned. Except, of course, if the return type is primitive, then the second argument is returned.

If you do not want to retrieve all the attributes, you can create this array manually. The Android standard attribute identifier is included in android.R.attr, while the attributes for this project are in R.attr.

 int attrsWanted[]=new int[]{android.R.attr.text, R.attr.textColor}; 

Please note that you should not use anything in the android.R.styleable file, as this may change in the future. The documentation still has a view of all these constants in one place.

Use it in layout files such as layout \ main.xml Include namespace declaration

XMLNS: application = "http://schemas.android.com/apk/res/com.mycompany.projectname"

in the top level xml element.

 <com.mycompany.projectname.MyCustomView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/transparent" android:text="Test text" android:textColor="#FFFFFF" app:extraInformation="My extra information"; /> 

Link to user view using full name.

LabelView Example for Android

If you want a complete example, take a look at the android label presentation sample.

LabelView.java

 TypedArray a=context.obtainStyledAttributes(attrs, R.styleable.LabelView); CharSequences=a.getString(R.styleable.LabelView_text); attrs.xml <declare-styleable name="LabelView"> <attr name="text"format="string"/> <attr name="textColor"format="color"/> <attr name="textSize"format="dimension"/> </declare-styleable> 

custom_view_1.xml

 <com.example.android.apis.view.LabelView android:background="@drawable/blue" android:layout_width="fill_parent" android:layout_height="wrap_content" app:text="Blue"app:textSize="20dp"/> 

This is contained in a LinearLayout with a namespace attribute:

XMLNS: application = "http://schemas.android.com/apk/res/com.example.android.apis"

+9
source

All Articles