android 使用xml定义自己的View

运行效果图:

主要activity:

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

}

主布局文件:

<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"

tools:context=".MainActivity"

android:orientation="vertical" >

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/hello_world" />

<com.example.mydefinedview2.MyView

android:layout_width="wrap_content"

android:layout_height="wrap_content">

</com.example.mydefinedview2.MyView>        <--      此为自定义view   -->

</LinearLayout>

自定义view:

public class MyView extends RelativeLayout{

Context context;

LayoutInflater inflate;

View view;

public MyView(Context context) {

super(context);

this.context = context;

initView();

}

public MyView(Context context, AttributeSet attrs) {

super(context, attrs);

this.context = context;

initView();

}

public void initView(){

inflate = LayoutInflater.from(context);

view = inflate.inflate(R.layout.my_view, null);

addView(view);

}

}

自定义view布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

tools:context=".MainActivity" >

<TextView

android:layout_width="200dp"

android:layout_height="200dp"

android:background="#f0f"

android:text="这是一个自定义View"

android:gravity="center" />

</RelativeLayout>

demo下载地址:

http://download.csdn.net/download/lyhdream/5210136

android 使用xml定义自己的View相关推荐

  1. android draw xml,Android使用XML定义渐变的drawable

    Android使用XML定义渐变的drawable Android,xml,drawable,渐变, 不是什么东西都需要美术来做图,利用Android提供的线程的东西,也能作出一下效果出来.当然比较负 ...

  2. Android中自定义View的研究 -- 在XML中引用自定义View

    如果在一直使用SetContentView(new HellwView(this)觉得总是少了一点东西,少了什么了,失去了Android中使用XML定义组件的便携性,这种感觉让人很不爽,呵呵,在这节里 ...

  3. android虚线如何定义xml,「Do.023」为啥用XML定义的虚线显示成了实线

    首发公众号:Android程序员日记 作者:贤榆的榆 如果喜欢,请 关注 | 赞赏 | 点在看 阅读时间:4978字 8分钟 前言 今天刚好是<千与千寻>在中国首映的日子.所以放一张< ...

  4. android 在xml文件中引用自定义View

    在xml中引用自定义view 方法一: [java] view plaincopy <com.test.copytext.CopyText android:layout_width=" ...

  5. android选项菜单xml,使用 XML 定义选项菜单

    何为选项菜单? 选项菜单是某个Activity的主菜单项,供您放置对应用产生全局影响的操作,如"搜索"."撰写电子邮件"和设置.选项菜单中的项目在屏幕上的显示位 ...

  6. Android TabHost和xml定义Menu应用

    Android TabHost和xml定义Menu应用 http://files.cnblogs.com/hnrainll/TabMenu.zip

  7. android 设置xml边框,XML定义实现圆角和边框

    我想大多数程序员都喜欢用代码解决问题.原因如下:用代码更加cool. 我美工不行,我会说出去吗? OK,好东西在这里. 2.1 基本的圆角.边框 Android除了支持原始的图片资源外,比较棒的一点就 ...

  8. 一起来学习android自定义控件3——边缘凹凸的View

    前言 最近做项目的时候遇到一个卡劵的效果,由于自己觉得用图片来做的话可以会出现适配效果不好,再加上自己自定义view方面的知识比较薄弱,所以想试试用自定义View来实现.先看设计图效果 实现分析 上面 ...

  9. Android Animation学习(六) View Animation介绍

    Android Animation学习(六) View Animation介绍 View Animation View animation系统可以用来执行View上的Tween animation和F ...

最新文章

  1. 64 安装_解决“不能安装 64 位Office,因已安装 32 位 Office 产品”问题
  2. 微服务架构之「 配置中心 」
  3. zip的用法python_Python学习之zip函数的用法
  4. NYOJ 5287 异 形 卵
  5. 业精于勤荒于嬉---Go的GORM查询
  6. python查看图像通道数(通过PIL)
  7. jboss4中手动部署EJB(jboss4.0.2+ejb2.0+j2sdk5.0+xpsp2)
  8. php+oracle新增数据类型,Oracle 修改某个字段的数据类型三种方式
  9. linux数据库能看到系统执行了哪些命令,DB2数据库在linux操作系统的指令有哪些?...
  10. 分步傅里叶算法_分布傅里叶算法求解非线性薛定谔的matlab程序问题
  11. Node.js模块化开发(非常详细,满满的干货)
  12. MYSQL 数据库配置优化
  13. 论文笔记:TABERT: Pretraining for Joint Understanding of Textual and Tabular Data
  14. 四川绵阳:充分利用区块链等技术,为农民工证照办理提供线上便捷服务
  15. python爬取谷歌图片_Python 爬取谷歌街景图片
  16. 华硕飞行堡垒扬声器没声音_华硕飞行堡垒FX50J音频系统是什么?FX50J有扬声器吗?...
  17. jquery canvas网页画布画图
  18. Awakening Your Senses【唤醒你的感觉官能】
  19. 论从容自信---张含韵和涛声依旧有感
  20. css 背景效果_前端教程 :20个CSS的常用套路附demo的效果实现与源码

热门文章

  1. C++11中值得关注的几大变化
  2. Oracle迁移至PostgreSQL工具之Ora2Pg
  3. PDF 补丁丁 0.6.0.3282 版发布(修复内存漏洞)
  4. Openswan企业实战之ipsec ***加速ERP系统
  5. 单例模式-3.透明的单例模式
  6. 重绘Winform窗体
  7. 函数学习-delattr()
  8. json数据解析详解---代码每行进行分析
  9. linux命令的学习
  10. nohup-真正的Shell后台运行