如何将当前的AttributeSet传递给自定义View类?如果我使用的参数中只有Context的构造函数,我将丢失所有主题,并在该自定义视图的xml中使用“style”标签的能力。

我所做的是创建一个包含我在xml文件中的自定义视图的活动,然后以编程方式创建一个新的,并将其添加到布局。我发现在xml中制作的样式是适当的样式,而我以编程方式创建的样式不是。

据我所知,两者之间的区别是系统使用CustomLayout1(Context context,AttributeSet attrs)构造函数。问题是我无法弄清楚如何让程序的AttributeSet传递给这个自定义视图,当我以编程方式创建它。

这是活动:

import android.app.Activity;

import android.os.Bundle;

import android.widget.LinearLayout;

public class ThemeOne extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

LinearLayout layout = (LinearLayout) findViewById(R.id.mainlayout);

layout.addView(new CustomLayout1(getApplicationContext()));

}

}

这是主要的xml:

android:orientation="vertical"

android:id="@+id/mainlayout"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:layout_width="fill_parent"

android:layout_height="wrap_content" />

自定义视图类:

import com.clearsync.test.theme1.R;

import android.content.Context;

import android.util.AttributeSet;

import android.view.LayoutInflater;

import android.widget.LinearLayout;

public class CustomLayout1 extends LinearLayout {

private Context context = null;

public CustomLayout1(Context context) {

super(context);

this.context = context;

create();

}

public CustomLayout1(Context context, AttributeSet attrs) {

super(context, attrs);

this.context = context;

create();

}

private void create(){

LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

layoutInflater.inflate(R.layout.inflateme, this, true);

}

}

最后,自定义视图xml:

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:layout_height="wrap_content"

android:text="Oh, Hewroh..."

style="?textview_style1" />

android attributeset 工具类,android – 如何将AttributeSet传递给自定义视图相关推荐

  1. android sharedpreferences 工具类,android sharedpreferences工具类

    释放双眼,带上耳机,听听看~! 今天,简单讲讲如何写一个sharedpreferences的工具类. 很简单,把一些重复的操作封装在工具类里,其他地方调用就可以.在网上搜索了比较多的资料,找到一个比较 ...

  2. android attributeset 工具类,Android使用AttributeSet自定义控件的方法

    释放双眼,带上耳机,听听看~! 所谓自定义控件(或称组件)也就是编写自己的控件类型,而非Android中提供的标准的控件,如TextView,CheckBox等等.不过自定义的控件一般也都是从标准控件 ...

  3. android 开发工具类,Android中常用开发工具类—持续更新...

    一.自定义ActionBar public class ActionBarTool { public static void setActionBarLayout(Activity act,Conte ...

  4. Android加密工具类,Android AES加密工具类分享

    1.AES加密工具类 java不支持PKCS7Padding,只支持PKCS5Padding.我们知道加密算法由算法+模式+填充组成,下一篇介绍iOS和Android通用的AES加密,本篇文章使用PK ...

  5. android dp工具类,Android 单位px、dp、sp转换工具类

    import android.content.Context; import android.util.TypedValue; //常用单位转换的辅助类 public DensityUtils { p ...

  6. android 计时器工具类,Android中通用定时器--好用的工具

    package com.utility.common; import android.os.Handler; import android.os.Message; public class BaseT ...

  7. android attributeset 工具类,android attributeset总结

    一般是当项目中遇到这样的场景需要自定义控件的AttributeSet属性:一个自定义控件的有些属性内容是随着外部条件而动态改变的,for example:一个自定义的ListView控件,需要在底部添 ...

  8. android sharedpreferences工具类

    今天,简单讲讲如何写一个sharedpreferences的工具类. 很简单,把一些重复的操作封装在工具类里,其他地方调用就可以.在网上搜索了比较多的资料,找到一个比较好的工具类. 参考文章:http ...

  9. 操作SD卡,获取文件目录及文件工具类Android,listView

    操作SD卡,获取文件目录及文件工具类Android 1.获取文件目录及文件工具类: DirectoryInfo  FileScan 1.1 DirectoryInfo: package com.gls ...

最新文章

  1. 一次失败的机巡平台对接经历分享
  2. 内核调试和系统调用劫持
  3. oracle bbed 使用,Oracle BBED使用 四步快速启动Oracle BBED
  4. 元类及创建元类的方法(面试用
  5. java构造方法 隐含三步_Java入门总结--------类和对象关系以及构造方法
  6. [Bzoj3233][Ahoi2013]找硬币[基础DP]
  7. ctguoj-取石头 (15分)
  8. 油库蓝牙+北斗RTK人员定位方案解析
  9. Linux基础——Web(三)Nginx反向代理2
  10. 看ftp服务器文件日期,ftp查看服务器当前日期
  11. PHP 使用fpdf,fpdi类库操作修改pdf文件,添加文字和图片水印,fpdf,fpdi添加中文文字乱码问题
  12. oracle取较小数,oracle 取小数位数
  13. 省市区areacode反查的精简写法
  14. 动手学习数据分析第一章内容
  15. 无线怎么联系不上服务器,无线路由器设置好后联系不到服务器
  16. 海思 Hi3516 使用 gpac 库把 H265 和 AAC 封装成 MP4
  17. 前端工程师高手说说CSS学习中的瓶颈
  18. PC_控制器_微程序控制器和硬布线控制器
  19. 过年“飞”回家?携带移动电源登机需注意!
  20. 布隆过滤器(Bloom Filter)、布谷鸟过滤器(Cuckoo Filter)、商过滤器(quotient filter)(持续更新~~~~)

热门文章

  1. 泛统计理论初探——KNN
  2. Spark SQL overwrite问题
  3. sessionfactory单例
  4. hibernate的sessionfactory
  5. 运算放大器积分电路及积分电路设计
  6. Autojs4.1.0实战教程---火火小视频极速版功能合集
  7. 一文讲懂什么是函数柯里化,柯里化的目的及其代码实现
  8. linux系统如何安装flash player
  9. 为什么二进制0代表正数,1代表负数
  10. 我的10年软件情缘--2001到2011