Android 自定义控件
2015-06-16 11:29
218 查看
Android 自定义控件
本文参考书籍:郭霖《第一行代码-Android》。关键字:自定义控件
需求
对于多个Activity,在界面顶部使用一个统一的标题栏,标题栏上:返回按钮-标题文字-编辑按钮,点击可响应操作简单分析
把这个标题栏做成一个控件,在每个Activity中使用xml定义标题栏样式
创建自己的控件类class
在主Activity的xml文件中使用自定义的控件
XML和代码文件
title_all.xml, 定义标题栏样式
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <Button android:id="@+id/title_btn_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="back" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_horizontal" android:text="this is the title" /> <Button android:id="@+id/title_btn_edit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="edit" /> </LinearLayout>
MyTitleLayout.java, 创建自己的控件类,包含样式和事件响应:
package com.example.androidcustomedviewtest; import android.app.Activity; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.LinearLayout; import android.widget.Toast; import com.myising99.androidcustomedview.R; public class MyTitleLayout extends LinearLayout { public MyTitleLayout(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater.from(context).inflate(R.layout.title_all, this); // 填充布局 Button btnBack = (Button) findViewById(R.id.title_btn_back); Button btnEdit = (Button) findViewById(R.id.title_btn_edit); // 注册事件 btnBack.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ((Activity) getContext()).finish(); } }); btnEdit.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getContext(), "clicked edit btn", Toast.LENGTH_SHORT).show(); } }); } }
activity_main.xml, 在主Activity的xml文件中使用自定义控件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.androidcustomedviewtest.MainActivity" > <!-- 自定义控件 com.example.androidcustomedview.MyTitleLayout, 需要指明控件的完整名 --> <com.example.androidcustomedviewtest.MyTitleLayout android:layout_width="match_parent" android:layout_height="wrap_content" > </com.example.androidcustomedviewtest.MyTitleLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="@string/hello_world" /> </LinearLayout>
MainActivity.java, 主Activity:
package com.example.androidcustomedviewtest; import android.app.Activity; import android.os.Bundle; import android.view.Window; import com.myising99.androidcustomedview.R; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); // 先去掉系统默认title bar setContentView(R.layout.activity_main); } }
当主Activity运行时,界面顶部就显示自定义的控件。
相关文章推荐
- 关于Android studio打包发布,以及Android签名的获取
- Android自定义控件(状态提示图表) (转)
- 查看Android内存的8中方法
- Android ListView滑动过程中图片显示重复错乱闪烁问题解决
- 八款开源Android游戏引擎
- Android首选项
- Android让文本自动滚动实现
- Android开发中的ANR问题如何避免?
- Android属性动画完全解析(下),Interpolator和ViewPropertyAnimator的用法
- android 比较靠谱的图片压缩
- android ContentObserver监听系统短信和备份短信到本地
- 一些有用的学习链接(AndroidStudio)
- Android PowerImageView实现,可以播放动画的强大ImageView
- android Handler 的使用
- android 技术总结 图片优化 sqlite使用 Fragment使用
- 一个帖子掌握android所有控件、ProgressBar 、Android 动画效果、SQLite、四大组件、Android多媒体
- Android-Universal-Image-Loader 图片异步加载类库的使用(超详细配置)
- Android Android环境安装
- 使EditText不获取焦点(Android中系统输入法不主动弹出)
- Android ListView使用BaseAdapter与ListView的优化