[Android新手学习笔记22]-如何自定义控件
来源:互联网 发布:大蒜种植面积数据 编辑:程序博客网 时间:2024/06/04 22:47
1.引入布局文件
右键res/layout文件夹,创建Layout Resource File,命名为title。配置代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#00ff00">
<Button
android:id="@+id/back_button"
android:text="返回"
android:textSize="30sp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="60dp" />
<TextView
android:id="@+id/text_view"
android:text="标题"
android:textSize="30sp"
android:layout_gravity="center"
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="60dp" />
<Button
android:id="@+id/edit_button"
android:text="编辑"
android:textSize="30sp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="60dp" />
</LinearLayout>
android:layout_gravity控制控件内容排列方式。
android:gravity控制控件排列方式。
然后在通过<include>标签引入,代码如下:
<?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">
<include layout="@layout/title" />
</LinearLayout>
在引用控件的Activity中,去掉标题栏,代码如下:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
}
}
2.添加方法
修改引入布局文件,代码如下:
<?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">
<top.xiexiaodong.uidemo.TitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
创建TitleLayout类,继承LinearLayout类,在这个类里面实现控件事件,代码如下:
public class TitleLayout extends LinearLayout {
public TitleLayout(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.title, this);
Button backButton = (Button) findViewById(R.id.back_button);
Button editButton = (Button) findViewById(R.id.edit_button);
backButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
((Activity) getContext()).finish();
}
});
editButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "你点击了编辑按钮", Toast.LENGTH_SHORT).show();
}
});
}
}
0 0
- [Android新手学习笔记22]-如何自定义控件
- [Android新手学习笔记30]-Broadcast-如何自定义广播
- Android学习笔记05: Android快速自定义控件+实战演示——新手进阶必看!
- 新手Android学习笔记 06——自定义控件的单击事件
- 【学习笔记】Android自定义控件
- android自定义控件学习笔记
- Android 自定义控件学习笔记
- Android-自定义控件学习笔记
- [Android新手学习笔记11]-UI控件之TextView
- [Android新手学习笔记12]-UI控件之EditText
- [Android新手学习笔记13]-UI控件之ImageView
- [Android新手学习笔记14]-UI控件之ProgressBar
- [Android新手学习笔记15]-UI控件可见性
- [Android新手学习笔记16]-UI控件之AlertDialog
- [Android新手学习笔记17]-UI控件之ProgressDialog
- android学习笔记之自定义控件
- [Android学习笔记]自定义控件的使用
- Android学习笔记:自定义控件篇
- 简单的日志打印工具
- Ajax:readyState(状态值)和status(状态码)的区别
- Swift 元组的定义和取值
- 1036. Boys vs Girls 解析
- 文件下载原理详解 http协议
- [Android新手学习笔记22]-如何自定义控件
- iOS UIRefreshControl
- git使用容易忽略的问题
- 卸载Oracle数据库完美解决方案
- code first数据库迁移
- 由浅入深探究mysql索引结构原理、性能分析与优化
- Struts2之文件下载
- 嵌入式软件测试的基本方法
- P07: 有依赖的背包问题