这定义一个控件,包含三个TextView:
控件xml:mywidget.xml
控件代码:
测试xml:main.xml
测试代码:
效果图:
[img]http://dl.iteye.com/upload/attachment/554727/295b9b62-487b-36ec-a806-3f6840d755d0.jpg[/img]
控件xml:mywidget.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:text="TextView" android:id="@+id/textView1"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:text="TextView" android:id="@+id/textView2"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:text="TextView" android:id="@+id/textView3"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
控件代码:
package com.tcl.testmywidget;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MyWidget extends LinearLayout{
private View mView;
/**
* 在xml中定义控件的构造函数
* @param context
* @param attrs
*/
public MyWidget(Context context, AttributeSet attrs) {
super(context, attrs);
mView = LayoutInflater.from(context).inflate(R.layout.mywidget, this, true);
}
/**
* 设置第一个textview
* @param string
*/
public void setText1(String string)
{
if(mView!=null)
{
TextView textView = (TextView) mView.findViewById(R.id.textView1);
textView.setText(string);
}
}
/**
* 设置第二个textview
* @param string
*/
public void setText2(String string)
{
if(mView!=null)
{
TextView textView = (TextView) mView.findViewById(R.id.textView2);
textView.setText(string);
}
}
public void setText3(String string)
{
if(mView!=null)
{
TextView textView = (TextView) mView.findViewById(R.id.textView3);
textView.setText(string);
}
}
/**
* 在代码中定义控件的构造函数
* @param context
*/
public MyWidget(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
}
测试xml:main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.tcl.testmywidget.MyWidget
android:id="@+id/mywidget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></com.tcl.testmywidget.MyWidget>
</LinearLayout>
测试代码:
package com.tcl.testmywidget;
import android.app.Activity;
import android.os.Bundle;
public class TestMyWidgetActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyWidget myWidget = (MyWidget) findViewById(R.id.mywidget);
myWidget.setText1("this is text1");
myWidget.setText2("this is text2");
myWidget.setText3("this is text3");
}
}
效果图:
[img]http://dl.iteye.com/upload/attachment/554727/295b9b62-487b-36ec-a806-3f6840d755d0.jpg[/img]