- 浏览: 180876 次
- 性别:
- 来自: 浙江
文章分类
最新评论
用代码简单介绍下自定义控件的使用:
先看主activity:
public class customImView extends LinearLayout{
public customImView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.customView);
String text = a.getString(R.styleable.customView_customtextStr);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.test_lin, this, true);
Button btn = (Button)view.findViewById(R.id.btn2);
btn.setText(text);
}
}
里面的样式styleable 来自于attrs.xml
<resources>
<declare-styleable name="customView">
<attr name="customtextSize" format="dimension"/>
<attr name="customtextStr" format="string"/>
</declare-styleable>
</resources>
布局文件来自于xml文件 test_lin.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="haha1111"/>
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="haha2222"/>
</LinearLayout>
主布局文件activity_main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:test="http://schemas.android.com/apk/res/com.lml.customview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<com.lml.customview.view.customImView
android:layout_width="match_parent"
android:layout_height="wrap_content"
test:customtextSize="100dp"
test:customtextStr="123啦啦啦"/>
</RelativeLayout>
注意这边的命名空间。
先看主activity:
public class customImView extends LinearLayout{
public customImView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.customView);
String text = a.getString(R.styleable.customView_customtextStr);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.test_lin, this, true);
Button btn = (Button)view.findViewById(R.id.btn2);
btn.setText(text);
}
}
里面的样式styleable 来自于attrs.xml
<resources>
<declare-styleable name="customView">
<attr name="customtextSize" format="dimension"/>
<attr name="customtextStr" format="string"/>
</declare-styleable>
</resources>
布局文件来自于xml文件 test_lin.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="haha1111"/>
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="haha2222"/>
</LinearLayout>
主布局文件activity_main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:test="http://schemas.android.com/apk/res/com.lml.customview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<com.lml.customview.view.customImView
android:layout_width="match_parent"
android:layout_height="wrap_content"
test:customtextSize="100dp"
test:customtextStr="123啦啦啦"/>
</RelativeLayout>
注意这边的命名空间。
- CustomView.zip (1.4 MB)
- 下载次数: 0
发表评论
-
关于Android的webSocket的简单使用
2017-05-12 14:34 917使用第三方jar: autobahn-0.5.0.jar 连 ... -
Comparator自定义排序的使用
2017-05-11 14:18 709java对于集合的自定义排序方法有: Arrays.sort ... -
查看Android虚拟机文件相关命令
2017-05-02 14:12 513我们在虚拟机下添加文件后,想查看下文件是否正确创建,可在win ... -
android6.0创建文件问题
2017-05-02 12:58 537Android在6.0有了动态权限管理,在文件创建时就需要动态 ... -
xutils的http模块的简单使用
2017-04-28 16:05 473先导入xutils相关依赖: compile 'org.xut ... -
Litepal的简单使用
2017-04-27 17:21 592相关包下载地址:https://github.com/Lite ... -
Android下拉刷新上拉加载控件的使用
2017-04-21 10:46 789参考链接:http://www.jianshu.com/p/6 ... -
图片加载框架
2017-04-19 16:29 361图片加载框架: picasso ImageLoader -
Android的Service总结
2017-04-17 15:46 412参考链接:http://www.cnblogs.com/lwb ... -
解决ViewPager的addOnPageChangeListener不加载第一个的问题
2017-03-18 17:53 2579今天在使用ViewPager的时候发现个问题。 需求如下: ... -
clone方法的使用
2017-01-04 10:14 511由于Java有引用这一说,当两个变量指向同一块内存时,改变 ... -
Fragment无法切换问题
2016-12-16 14:57 2067Android有一个回收机制,当内存不足时,会自动回收相关内存 ... -
关于Material Design的CollapsingToolbarLayout初次使用
2016-12-16 13:38 568最近了解了下CollapsingToolbarLayout的使 ... -
MVP模式的学习
2016-12-10 15:15 610以前我写代码都是使用MVC模式,这种模式使Activity变得 ... -
Material Design:Android Design Support Library 介绍
2016-12-10 14:14 420参考链接 : https://blog.leancloud.c ... -
SpannableString简介
2016-12-10 14:03 378参考链接: http://www.cnblogs.com/ji ... -
getResources().getDrawable方法的废弃
2016-12-10 13:20 1155参考链接:http://www.jianshu.com/p/e ... -
关于AndroidStudio的Unsupported major.minor version 52.0异常
2016-12-10 13:15 2332参考链接:http://blog.csdn.net/fakin ... -
Android记录
2015-06-01 10:54 599http://tools.android-studio.org ... -
android使用html标签
2015-03-13 20:42 633有些时候我们需要在一个textview上,对部分字体进行特殊效 ...
相关推荐
《Android自定义控件开发入门与实战》高清影印版,带目录,拒绝压缩包加密坑钱,内容深入浅出,对于有Android自定义开发需求的非常有帮助
Android自定义控件开发入门与实战从自定义基础到实战的讲解。一步步深入。适合有一定Android基础的读者。本压缩包中自带了推荐的pdf阅读器。大家要是喜欢这本文档,推荐去京东,天猫,当当买支持一下默默付出的作者...
Android自定义控件示例,基于Android studio 1.0.2。
1 本书从动画、绘图、视图三方面介绍Android自定义控件相关知识,内容系统全面,并配以翔实的案例。 2 Android自定义控件涉及动画和色彩,本书将图片地址制作成二维码,可供读者扫描观看。 3 本书适合初高级水平从业...
【Android进阶】(1)用继承和组合方式自定义控件 相关工程。
Android 自定义控件简单Demo 图片+文字
Android自定义控件源码.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
Android自定义控件步骤总结 http://www.cnblogs.com/duanxz/p/3625589.html
教程名称: 尚硅谷Android视频教程《自定义控件》Android自定义控件,是中高级程序员必须熟练掌握的技术之一。本套视频涵盖了安卓自定义开发过程中所有的技术问题,课程中讲授的例子全部来源于企业。学习本套视频后...
android 自定义控件 小球的圆周运动 canvas绘出
android自定义控件CircleImageVIew的使用举例
Android 组合控件,用自定义方式实现,非简单重写adapter。同时包含了点击事件的响应。
Android自定义控件详细开发教程
Android自定义控件之滑动开关 Android自定义控件之滑动开关 http://blog.csdn.net/eeeduo/article/details/37606567
详细讲解自定义控件文章的配套代码!文章地址:http://blog.csdn.net/dinglin_87/article/details/7431545
Android自定义控件源码含APK 仿Material Design风格,在低版本android环境上面实现高版本中的按钮等UI控件,视觉效果提升,本源码将向你介绍一些方法,实现这种效果。注:本源码中使用的控件是原作者已经封装好的,...
android自定义控件
android 自定义控件的使用.doc
android 自定义控件代码实现。有需要自定义按钮的,请下载使用,供学习