在Layout xml中配置自定义View
2011-07-28 01:05
309 查看
1. Layout xml文件中的配置
main.xml
2. 自定义的View,这里以继承ListView为例
MyListView.java
对于ListView这样做的好处是,不需要将Adapter的子类定义置于ListView所在的父Activity类中。增强ListView的模块化,从而降低耦合,增强复用性。
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <com.larry.testbuild.MyListView android:id="@+id/my_list" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </RelativeLayout>注意在引用自定义的View类时,要写出类的全路径,即包名.类名
2. 自定义的View,这里以继承ListView为例
MyListView.java
package com.larry.testbuild public class MyListView extends ListView { public DishListView(Context context) { super(context); } public DishListView(Context context, AttributeSet attrs) { super(context, attrs); } ... }注意需要继承父类的两个构造函数,少一个都会造成Force close。
对于ListView这样做的好处是,不需要将Adapter的子类定义置于ListView所在的父Activity类中。增强ListView的模块化,从而降低耦合,增强复用性。
相关文章推荐
- 在Layout xml中配置自定义View
- 自定义shape的view,不用配置xml
- 如何在xml中配置自定义view以及使view动起来
- LayoutInflater和inflate()方法的用法 (自定义View中加载xml布局)
- Android解析自定义属性的XML实现底部导航栏TabSelectedView,实现灵活的配置扩展
- 自定义的LinearLayout 使用报错 android.view.InflateException: Binary XML file line #3: Error inflating class
- Android中layout.xml文件中加载自定义的View类
- 自定义View冷知识之动态替换layout.xml中的View
- LayoutInflater和inflate()方法的用法 (自定义View中加载xml布局)
- 通过自定义view替代layout.xml表示activity的界面
- Android学习个人笔记1-Layout界面布局-xml,java混合模式-自定义view
- TabLayout 自定义view tab 消除间距的解决方案
- 自定义UICollectionViewLayout
- 自定义View(3)——onLayout()、onDraw()方法的简单使用
- 在ssh框架下web.xml配置404和500错误代码,自定义不生效的解决办法。
- 自定义Layout实现Android 5.0 Material Design的点击任意View的水波效果
- Android ImageView配置android:layout_height="wrap_content"时上下会有padding存在
- TabLayout + ViewPager + Fragment + 百度地图 + 传感器 + 自定义View + AIDL
- 自定义View(三) 之 onMeasure() onLayout() onDraw()
- 35-002-1 struts前期之 通过JAVA反射机制及XML配置文件解析自定义一个MVC的简单框架