Android进阶之路 - 自定义控件的简单使用(一)
2017-01-08 19:13
316 查看
本篇带给大家的是include的简单使用,还有初级自定义控件的使用方式
关于Include的使用方式,大家可以通过下面链接进行学习:
http://blog.csdn.net/qq_20451879/article/details/54288317
UI结构:
1.include视图的复用
2.自定义控件事件响应
3.最基本的自定义控件引用
目录:
1.MainActivity与TitleCostom、CostomLayout代码
2.各自的Xml布局
3.注意点(可能会帮你更快速的熟练使用)
MainActivity:
CostomLayout(无事件响应):
TitleCostom(事件响应):
MainActivity的Xml:
CostomLayout的Xml:
4000
/pre>
TitleCostom的Xml:
关于Include的使用方式,大家可以通过下面链接进行学习:
http://blog.csdn.net/qq_20451879/article/details/54288317
UI结构:
1.include视图的复用
2.自定义控件事件响应
3.最基本的自定义控件引用
目录:
1.MainActivity与TitleCostom、CostomLayout代码
2.各自的Xml布局
3.注意点(可能会帮你更快速的熟练使用)
MainActivity:
package com.example.costomdemo; import android.os.Bundle; import android.annotation.SuppressLint; import android.app.ActionBar; import android.app.Activity; @SuppressLint("NewApi") public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //去掉ActinBar,也可以自己在清单配置 ActionBar actionBar = getActionBar(); if (actionBar!=null) { actionBar.hide(); } } }
CostomLayout(无事件响应):
package com.example.costomdemo; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.widget.FrameLayout; /** * 最简单的自定义控件 * */ public class CostomLayout extends FrameLayout{ public CostomLayout(Context context, AttributeSet attrs) { super(context, attrs); //这里其实要要注意的还是参数问题,指的是Inflate的第二个参数,它属于ViewGroup,这个时候我们不可以像其实时候填充为null,而应该指出是当前的布局,所以添为上下文 LayoutInflater.from(context).inflate(R.layout.costom_xml, this); } }
TitleCostom(事件响应):
package com.example.costomdemo; 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; /** * 自定义控件的事件处理 * */ public class TitleCostom extends LinearLayout { public TitleCostom(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater.from(context).inflate(R.layout.title_xml, this); Button mButton = (Button) findViewById(R.id.title_costom); mButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getContext(), "自定义控件入门使用", 0).show(); } }); } }
MainActivity的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" > <include layout="@layout/include_xml" android:layout_width="match_parent" android:layout_height="20dp" /> <com.example.costomdemo.TitleCostom android:layout_width="match_parent" android:layout_height="30dp" /> <com.example.costomdemo.CostomLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout>
CostomLayout的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="vertical" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="Costom初级页面" android:gravity="center" /> </LinearLayout><
4000
/pre>
TitleCostom的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:layout_width="match_parent" android:layout_height="wrap_content" android:text="自定义入门" android:textSize="10sp" android:id="@+id/title_costom" android:gravity="center" /> </LinearLayout>
include复用的layout:<?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="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Include简单的视图复用" android:textSize="10sp" android:gravity="center" /> </LinearLayout>
注意点:
1、首先在Xml中使用include标签,需要有对应的layout,不需要绑定对应的Activity、Fragment等,但是无法做事件响应这是一个缺点,不过复用性比较可观,如果要单独事件响应,需要设置id,这个大家可以跳转我们上面的链接进行了解
2、使用自定义控件一定要记得继承(extends)Layout
3、布局填充LayoutInflater.from(context).inflate(R.layout.costom_xml, this);第二个参数是Viewgrop所以一定注意
4、在对应布局引用的时候一定记得copy对应的包名,类名,因为它本身已经被我们定义为一个控件,好比你使用TextView之类的是一样的,为了提醒你,send you a photo;
相关文章推荐
- Android进阶之路 - RecyclerView左划删除(SwipeRecyclerView的简单使用)
- Android进阶之路 - Fragment(碎片化)、进栈、不进栈的简单使用(一)
- Android进阶之路 - Picasso的简单使用与工具封装
- Android自定义控件的简单使用
- Android简单了解自定义控件和自定义属性如何使用
- Android进阶之路 - Glide的简单使用与自封工具
- Android自定义控件---TabIndicatord选项卡按钮(可用于ViewPager,使用简单)
- android 使用MideaPlayer API来播放简单的应用
- Android Service使用方法--简单音乐播放实例
- android自定义控件的创建和使用
- Android最简单的图片裁剪方法,非使用系统裁切功能
- Android Google Map的简单使用(一)
- 【Android笔记 三】Location获取地理位置信息(中)Criteria类的简单使用
- Android declare-styleable:自定义控件的属性(attr.xml,TypedArray)的使用
- APIDemo学习笔记——Android上几种简单的Animation使用方法(一)
- android自定义控件的创建和使用
- APIDemo学习笔记——Android上几种简单的Animation使用方法(二)
- Android上使用Google Maps最最简单的例子
- android google map 自定义地图的图层 简单使用(二)
- APIDemo学习笔记——Android上几种简单的Animation使用方法(三)——3D Transition