android组合自绘Widget(2D)
来源:互联网 发布:淘宝靠谱点的军品店 编辑:程序博客网 时间:2024/05/21 06:24
现在介绍另一种自绘方式,这种方式利用的原有的Widget,将這些Widget组合起来形成自己的Widget(如由Edit和Button等组成的下拉列表框)。
下面是如何创建组合Widget的步骤:
<!--[if !supportLists]-->1. <!--[endif]-->创建一个从Layout继承过来的子类,比如LinearLayout
<!--[if !supportLists]-->2. <!--[endif]-->在新创建的类里面的构造函数中,有三个构造函数,一个参数的构造函数主要是让使用者能动态建立,而两个参数的构造函数则是用于XML配置的
<!--[if !supportLists]-->3. <!--[endif]-->在你创建的子WIDGET中可以创建事件监听以来响应相应的事件
<!--[if !supportLists]-->4. <!--[endif]-->创建你自己的访问器和修改器
<!--[if !supportLists]-->5. <!--[endif]-->虽然你继承了Layout,但你不一定需要重载onDraw和onMeasure,Layout会有一些默认的行为让继承的子类可以正常运行,当然,如果你需要重载改变某些行为的话,你也可以重载。
<!--[if !supportLists]-->6. <!--[endif]-->你也可以重载onKeyDown之类的方法,实现相应的功能
下面是一个demo:
建立自己的WIDGET:
package com.OwnWidget.OwnButton;
import android.content.Context;
importandroid.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
publicclass OwnLayoutextends LinearLayout
{
private TextViewmText;
private ButtonmButton;
privateint mTag = 0;
public OwnLayout(Context context)
{
super(context);
InitLayoutWidget(context);
//TODO Auto-generatedconstructor stub
}
public OwnLayout(Context context, AttributeSet attrs)
{
super(context, attrs);
InitLayoutWidget(context);
//TODO Auto-generatedconstructor stub
}
privatevoid InitLayoutWidget(Context context)
{
this.setOrientation(HORIZONTAL);
mText =new TextView(context);
mText.setText("JackLam");
addView(mText,new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
mButton =new Button(context);
mButton.setText("确定");
addView(mButton,new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
mButton.setOnTouchListener(new OnTouchListener()
{
@Override
publicboolean onTouch(View v, MotionEvent event)
{
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
if(mTag == 0)
{
mText.setText("Love");
mTag++;
}
else if(mTag == 1)
{
mText.setText("Elaine");
mTag++;
}
else if(mTag == 2)
{
mText.setText("Love");
mTag++;
}
else
{
mText.setText("JackLam");
mTag = 0;
}
v.invalidate();
break;
case MotionEvent.ACTION_UP:
break;
default:
break;
}
returntrue;
}
});
}
}
在activity的xml中加上自己的Widget:
<?xmlversion="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<com.OwnWidget.OwnButton.OwnButton
android:id="@+id/widget_myButton"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content">
</com.OwnWidget.OwnButton.OwnButton>
<com.OwnWidget.OwnButton.OwnLayout
android:id="@+id/widget_myLayout"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content">
</com.OwnWidget.OwnButton.OwnLayout>
</LinearLayout>
在Activity中使用:
package com.OwnWidget;
importcom.OwnWidget.OwnButton.OwnButton;
import android.app.Activity;
import android.os.Bundle;
importandroid.widget.LinearLayout;
publicclass OwnWidgetextends Activity {
/** Calledwhen the activity is first created. */
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*LinearLayout layout = newLinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
OwnButtonbtn = newOwnButton(this);
layout.addView(btn);
setContentView(layout);*/
setContentView(R.layout.main);
}
}
下一章,继续会介绍自绘WIDGET,通过更改一个已存在view。
- android组合自绘Widget(2D)
- android完全自绘Widget(2D)
- android自绘Widget(2D)之修改存在的WIDGET
- android widget的中文文档 (自译)
- android widget(1)
- 剖析Android自带Widget-Music播放器
- Android组件(widget)Raidobutton
- android widget学习(三)
- Android---App Widget(一)
- Android自定义控件(Widget)
- Android常用控件(Widget)
- Android中的自绘View的那些事儿(四)之 ComposeShader组合渲染器的简介
- Android学习笔记:Home Screen Widgets(2):关于Widget
- android widget
- android widget
- android widget
- [android] widget
- android widget
- 在两台Linux机器之间配置一条SLIP链路,以便使用互联网socket进行通信
- 开始失望了
- Android中Intent详解
- android 启动 service 的两种方法
- android:MIME 参考手册
- android组合自绘Widget(2D)
- Android Parcel理解
- [ZZ]valgrind 手册
- 对Java中四种基本排序的总结
- android自绘Widget(2D)之修改存在的WIDGET
- [ZZ] Valgrind--Linux下的内存调试和代码解剖工具
- VC++:如何将程序启动时最小化到托盘
- 用log4erl怎么获取 error_logger捕获到的OTP系统的错误信息?
- undefined reference to `ts_read_raw'