본문 바로가기

Program/Android

굴욕...BaseLayout Custom

http://www.permadi.com/blog/2010/03/android-sdk-using-custom-view-in-xml-based-layout/

Creating The Custom View Class

To begin with, let’s create a custom view class. For demonstration purpose, the one I am using is below:

01 package com.permadi.example;
02
03 import android.content.Context;
04 import android.util.AttributeSet;
05 import android.view.LayoutInflater;
06 import android.view.View;
07 import android.widget.LinearLayout;
08  
09 public class CustomView extends LinearLayout
10 {
11  public CustomView(Context context, AttributeSet attrs)
12  {
13    super(context, attrs);
14  
15    LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
16    View view=layoutInflater.inflate(R.layout.example,this);
17  }
18 }

This class loads the XML file named example.xml in the line:

1 View view=layoutInflater.inflate(R.layout.example,this);

It is not necessary for the custom view class to be associated with an XML like above (this topic is explained here:http://permadi.com/blog/2010/03/android-sdk-loading-layout-and-associating-to-a-class/), you can use other techniques, such as constructing the view manually in the code.

example.xml looks like this:

01 <TableLayout android:id="@+id/TableLayout01"
02    android:layout_width="fill_parent"
03    android:layout_height="fill_parent"
04    xmlns:android="http://schemas.android.com/apk/res/android">
05   <TableRow android:id="@+id/TableRow01"
06      android:layout_width="wrap_content"
07      android:layout_height="wrap_content">
08      <TextView android:id="@+id/TextView01"
09          android:layout_width="wrap_content"
10          android:layout_height="wrap_content"
11          android:textSize="40px"
12          android:text="Questionaire">
13      </TextView>
14      <RadioButton android:id="@+id/RadioButton01"
15          android:layout_width="wrap_content"
16          android:layout_height="wrap_content"
17          android:text="Yes" android:textColor="#0f0"
18          android:layout_gravity="right"
19          android:layout_alignParentRight="false">
20      </RadioButton>
21      <RadioButton android:id="@+id/RadioButton02"
22          android:layout_width="wrap_content"
23          android:layout_height="wrap_content"
24          android:text="No" android:textColor="#f00"
25          android:layout_gravity="right" android:layout_alignParentLeft="false"
26          android:layout_alignParentRight="true"></RadioButton>
27    </TableRow>
28 </TableLayout>

And the layout generated by above xml is shown below:

Adding The Custom View Class To A Layout XML

To use the custom class in another xml based layout, you can refer to it by the fully qualified class name (fully qualified means:com.example.permadi.CustomView or whatever the package name plus the class name is for your custom class, and not just the class name). For example, let’s use com.example.permadi.CustomView on main.xml. (Note: In Eclipse, with the current version of SDK, you cannot drag and drop the custom view object into the editor, instead you need to enter the fully qualified class name manually into the xml file.)

1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout
3   xmlns:android="http://schemas.android.com/apk/res/android"
4   android:id="@+id/container"
5   android:layout_height="fill_parent" android:orientation="vertical" android:layout_width="fill_parent">
6     <com.example.permadi.CustomView android:layout_height="wrap_content" android:layout_width="fill_parent"/>
7     <com.example.permadi.CustomView android:layout_height="wrap_content" android:layout_width="fill_parent"/>
8     <com.example.permadi.CustomView android:layout_height="wrap_content" android:layout_width="fill_parent"/>
9 </LinearLayout>

You can see that I created 3 instances of the CustomView in the main.xml – of course you can add more as needed.

Important: You must specify the layout_width and layout_height if you are not using the TableLayout, otherwise an exception will be thrown and the application will crash. Once you set the layout_width and layout_height, you can even use Eclipse editor to edit and see a preview:

In a real application, you might want to assign an unique id for each so that you can easily retrieve the instance(s), but in this example, I will simply load the main.xml and use it with setContentView in an Activity class.

01 package com.example.permadi;
02  
03 import android.app.Activity;
04 import android.os.Bundle;
05  
06 public class MyActivity extends Activity {
07     /** Called when the activity is first created. */
08     @Override
09     public void onCreate(Bundle savedInstanceState) {
10         super.onCreate(savedInstanceState);
11         setContentView(R.layout.main);
12     }
13 }

Here’s how the example looks in Nexus One:

I hope this tips is useful to help speeding up development time. The example project, created with Eclipse can be found here:

http://permadi.com/blog/2010/03/android-sdk-using-custom-view-in-xml-based-layout-download-example-project/ 

'Program > Android' 카테고리의 다른 글

Android - WebView & JavaScript  (1) 2011.11.14
Task or Stack  (1) 2011.11.07
동적 뷰 추가하기  (2) 2011.10.29
LayoutInflater  (1) 2011.10.29
SQLite Database Browser - Android & iOS  (0) 2011.10.24