Android学习笔记--6.自定义控件的简单使用
来源:互联网 发布:java学生成绩 编辑:程序博客网 时间:2024/06/07 11:53
前言:在自定义控件的学习中遇到了两个小问题,第一个是隐藏标题栏的时候写了代码没有起到效果,后来发现Android Studio创建活动的时候继承的是AppCompatActivity,书上的requestWindowFeature这个其实是不适用的,应该使用这个supportRequestWindowFeature方法才可以。还有一个问题是,在引用自定义控件的时候,提示找不到包名,刷新一下后发现还是不可以,最后用真机直接运行发现,是可以显示的。然后再说说这个自定义控件,我的写的简单的例子是带有响应事件的标题栏,大家都知道标题栏是经常使用的,所以把标题栏写成了自定义控件。
1.创建一个CustomLayout布局文件
<?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="wrap_content" android:background="#f0f0"><Button android:id="@+id/back_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:background="#f0f0" android:textColor="#fff" android:layout_margin="5dp" android:text="返回" /> <TextView android:id="@+id/title_textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="主页" android:textColor="#fff" android:textSize="24sp" android:gravity="center" android:layout_weight="1" /> <Button android:id="@+id/edit_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:background="#f0f0" android:text="编辑" android:textColor="#fff" android:layout_margin="5dp"/></LinearLayout>
2.创建TitleLayout类,继承于LinearLayout“
这里主要是重写TitleLayout的构造参数,使用LayoutInflater.from()方法构造一个对象,然后用inflate()方法添加刚才写的布局文件,第二个参数是指把布局添加到当前的类上,即这个类成为了父布局。
package com.example.administrator.uilayouttest;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;/** * Created by Administrator on 2016/9/5. */public class TitleLayout extends LinearLayout { public TitleLayout(Context context, AttributeSet attrs){ super(context,attrs); LayoutInflater.from(context).inflate(R.layout.customlayout,this); Button mBackButton = (Button)findViewById(R.id.back_btn); Button mEditButton = (Button)findViewById(R.id.edit_button); mBackButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ((Activity)getContext()).finish(); } }); mEditButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getContext(), "you click editBtn", Toast.LENGTH_SHORT).show(); } }); }}
3.在主布局文件中引入带有响应事件的自定义控件
<?xml version="1.0" encoding="utf-8"?><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"> <com.example.administrator.uilayouttest.TitleLayout android:layout_width="match_parent" android:layout_height="wrap_content" /></LinearLayout>
4.主活动的代码
package com.example.administrator.uilayouttest;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.support.v7.widget.AppCompatPopupWindow;import android.view.Window;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // requestWindowFeature();不支持AppCompatActivity supportRequestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); }}
0 0
- Android学习笔记--6.自定义控件的简单使用
- [Android学习笔记]自定义控件的使用
- Android学习笔记(十五)自定义控件(declare-styleable 的使用)
- 【学习笔记】Android自定义控件
- android自定义控件学习笔记
- Android 自定义控件学习笔记
- Android-自定义控件学习笔记
- Android笔记--简单的自定义View之自绘控件
- Android学习笔记--自定义Adapter的使用
- ASP.NET学习笔记--自定义验证控件的使用
- android 自定义view学习笔记————Paint和Canvas的简单使用
- android UI(2)自定义控件简单使用
- Android笔记—Listview控件的自定义使用
- android自定义控件的学习
- android学习笔记之自定义控件
- Android学习笔记:自定义控件篇
- Android学习笔记之自定义控件--标题栏
- android学习笔记--创建自定义控件
- ASP与 ASP.NET以及 ASP.NET MVC的关系
- 在spring中使用JDBC事务配置
- CodeForces 707B-Bakery
- 关于php访问mssql2008过程中出现的问题
- Activity的启动模式以及onNewIntent(Intent intent)
- Android学习笔记--6.自定义控件的简单使用
- PHP服务器Apache虚拟主机配置
- 依赖注入库 Dagger2 的使用方法
- [343]Integer Break
- 网站访问使用HttpClien出现SSl证书问题
- C++编程入门系列之十四(类与对象:构造函数和析构函数)
- 已知二叉树的前序遍历和中序遍历,如何得到它的后序遍历?
- iOS9 获取手机通讯录及contact框架的使用
- 230. Kth Smallest Element in a BST