Android自定义控件的设计流程
来源:互联网 发布:skycc营销软件 编辑:程序博客网 时间:2024/05/17 22:24
1.在strings.xml文件中,添加自定义声明属性
<?xml version="1.0" encoding="utf-8"?><resources> <string name="hello">Hello World, SplashActivity!</string> <string name="app_name">Mobilesafe</string> <declare-styleable name="my_setting"> <attr name="content" format="string"></attr> <attr name="title" format="string"></attr> </declare-styleable></resources>2.在自定义布局文件my_view.xml中,引用该申明属性
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:woo="http://schemas.android.com/apk/res/com.woo.mobilesafe" android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/tv_myview_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="3dip" android:layout_marginTop="2dip" android:text="设置的标题" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/tv_myview_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_myview_title" android:layout_marginLeft="3dip" android:layout_marginTop="2dip" android:text="设置的具体内容" /> <CheckBox android:id="@+id/cb_myview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:clickable="false" android:focusable="false" /></RelativeLayout>
3.在相对布局的.java文件中编辑属性package com.woo.mobilesafe.ui;import com.woo.mobilesafe.R;import android.content.Context;import android.content.res.TypedArray;import android.util.AttributeSet;import android.view.View;import android.widget.CheckBox;import android.widget.RelativeLayout;import android.widget.TextView;public class SettingView extends RelativeLayout {private TextView tv_title;private TextView tv_content;private CheckBox cBox;public SettingView(Context context) {super(context);//挂载布局文件到 当前SettingView中 View view=View.inflate(context, R.layout.my_view, this); tv_content=(TextView) view.findViewById(R.id.tv_myview_content); tv_title=(TextView) view.findViewById(R.id.tv_myview_title); cBox=(CheckBox) view.findViewById(R.id.cb_myview);}public SettingView(Context context, AttributeSet attrs) {super(context, attrs); View view=View.inflate(context, R.layout.my_view, this); tv_content=(TextView) view.findViewById(R.id.tv_myview_content); tv_title=(TextView) view.findViewById(R.id.tv_myview_title); cBox=(CheckBox) view.findViewById(R.id.cb_myview); //获取my_setting.xml解析的数据 TypedArray a=context.obtainStyledAttributes(attrs,R.styleable.my_setting); String content=a.getString(0); String title=a.getString(1); tv_content.setText(content); tv_title.setText(title); a.recycle();}public boolean isChecked(){return cBox.isChecked();}public void setTitle(String text){tv_title.setText(text);}public void setContent(String text){tv_content.setText(text);}}4.在布局文件setting.xml中应用自定义的控件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:itheima="http://schemas.android.com/apk/res/com.woo.mobilesafe" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="40.0dip" android:background="#ff6cbd45" > <TextView android:id="@+id/tv_main_title" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:text="设 置 中 心" android:textColor="#ffffff" android:textSize="18.0dip" /> </LinearLayout> <cn.itcast.mobilesafe.ui.SettingView android:id="@+id/sv_autoupdate" android:layout_width="match_parent" android:layout_height="wrap_content" itheima:content="自动更新没有开启" itheima:title="自动更新设置" > </cn.itcast.mobilesafe.ui.SettingView></LinearLayout>5.在SettingActivity中调用setting.xml的布局
package com.woo.mobilesafe;import android.app.Activity;import android.os.Bundle;public class SettingActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//引入setting的布局setContentView(R.layout.setting);}}
6.在清单文件中配置activity.
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.woo.mobilesafe" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8" /> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <application android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen" android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name="com.woo.mobilesafe.SplashActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MainTabActivity"></activity> <activity android:name=".SettingActivity"></activity> </application></manifest>
7.在主页面中设置onclick事件
package com.woo.mobilesafe;import com.woo.mobilesafe.adapter.MainTabAdapter;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.GridView;public class MainTabActivity extends Activity {private GridView gv_main;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);gv_main=(GridView) findViewById(R.id.gv_main);//设计数据适配器, gridview数据绑定。gv_main.setAdapter(new MainTabAdapter(this));//图片项目点击事件gv_main.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position,long id) {switch (position) {case 8:Intent intent= new Intent(MainTabActivity.this,SettingActivity.class);startActivity(intent);break;}}});}}
0 0
- Android自定义控件的设计流程
- 自定义控件的流程
- Android自定义控件 ----- 基本绘制流程,简单控件的实现
- Android 自定义控件之View的绘制流程
- 自定义控件的使用流程
- 自定义控件的设计
- Android 绘制流程分析与自定义控件
- Android-UI控件的绘制流程以及自定义控件的具体操作
- Android仿微信录音功能,自定义控件的设计技巧
- android的自定义控件
- android的自定义控件
- View和自定义控件的绘制流程
- android 自定义view的流程
- Android 自定义UI组合控件设计方法
- 自定义控件开发流程
- 自定义控件编写流程
- 自定义控件编写流程
- 自定义组合控件流程
- zlib
- Linq to SQL: 自定义排序
- iOS程序的Build过程
- Linux下安装redis
- linux下的yum安装
- Android自定义控件的设计流程
- mysql command line client 使用命令
- IOS中创建使用链接库(总结)
- 快速的批量修改重命名.net程序的命名空间
- Android 常用的Intent的URI及示例
- django 学习点滴
- Android MediaPlayer简介
- 黑马程序员_IO包中的其他类
- 在Lua脚本中使用OpenGL函数