如何通过的AttributeSet自定义视图 [英] How to pass AttributeSet to custom View

查看:129
本文介绍了如何通过的AttributeSet自定义视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何通过当前的AttributeSet到自定义视图类?如果我使用一个构造函数,只有背景下的争论,我失去了所有的主题,并使用风格标签的XML中的自定义视图的能力。

我所做的就是创建一个包含我的自定义视图已经在XML文件中的一个活动,然后以编程方式创建一个新的,并把它添加到布局。我觉得是做在XML中的人有适当的造型,这也是我创建编程没有。

这两者之间的差别,据我可以告诉的是,该系统采用了 CustomLayout1(上下文的背景下,ATTRS的AttributeSet)的构造。问题是我无法弄清楚如何获得的AttributeSet应用程序传递到时候我编程方式创建这个定制视图。

这里的活动:

 进口android.app.Activity;
进口android.os.Bundle;
进口android.widget.LinearLayout;

公共类ThemeOne延伸活动{

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        的LinearLayout布局=(的LinearLayout)findViewById(R.id.mainlayout);

        layout.addView(新CustomLayout1(getApplicationContext()));
    }
}
 

下面是主要的xml:

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
 机器人:方向=垂直
 机器人:ID =@ + ID / mainlayout
 机器人:layout_width =FILL_PARENT
 机器人:layout_height =FILL_PARENT>
 < com.clearsync.test.theme1.CustomLayout1机器人:ID =@ + ID / maincustom
  机器人:layout_width =FILL_PARENT
  机器人:layout_height =WRAP_CONTENT/>
< / LinearLayout中>
 

自定义视图类:

 进口com.clearsync.test.theme1.R;

进口android.content.Context;
进口android.util.AttributeSet;
进口android.view.LayoutInflater;
进口android.widget.LinearLayout;

公共类CustomLayout1扩展的LinearLayout {
 私人上下文的背景下= NULL;

 公共CustomLayout1(上下文的背景下){
  超(上下文);
  this.context =背景;
  创建();
 }

 公共CustomLayout1(上下文的背景下,ATTRS的AttributeSet){
  超(背景下,ATTRS);
  this.context =背景;
  创建();
 }

 私人无效创建(){
  LayoutInflater layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  layoutInflater.inflate(R.layout.inflateme,这一点,真正的);
 }
}
 

最后,自定义视图的xml:

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
 机器人:方向=垂直
 机器人:layout_width =FILL_PARENT
 机器人:layout_height =FILL_PARENT>
 < TextView的Andr​​oid的:layout_width =WRAP_CONTENT
  机器人:layout_height =WRAP_CONTENT
  机器人:文本=哦​​,Hewroh ......
  风格=?textview_style1/>
< / LinearLayout中>
 

相反,与layout.addView构建它的

解决方案

(新CustomLayout1(getApplicationContext()));在你的活动中LayoutInflater它充气。

  LayoutInflater吹气= LayoutInflater.from(本);
inflater.inflate(R.layout.yourcustomviewxml,布局);
 

How do I pass the current AttributeSet to a custom View class? If I use a constructor that only has Context in the arguments, I lose all themes and the ability to use "style" tags in the xml for that custom View.

What I've done is create an activity that contains my custom view already in the xml file, and then programmatically create a new one and add it to the layout. What I find is the one that is made in the xml has the proper styling, and the one I create programmatically doesn't.

The difference between the two as far as I can tell is that the system uses the CustomLayout1(Context context, AttributeSet attrs) constructor. The problem is I can't figure out how to get the AttributeSet for the application to pass to this custom view when I create it programmatically.

Here's the Activity:

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()));
    }
}

Here's the main xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" 
 android:id="@+id/mainlayout"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <com.clearsync.test.theme1.CustomLayout1 android:id="@+id/maincustom"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content" />
</LinearLayout>

The custom view class:

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);
 }
}

and finally, the custom view xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" 
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <TextView android:layout_width="wrap_content"
  android:layout_height="wrap_content" 
  android:text="Oh, Hewroh..."
  style="?textview_style1" />
</LinearLayout>

解决方案

Instead of building it with layout.addView(new CustomLayout1(getApplicationContext())); inflate it with the LayoutInflater in your Activity.

LayoutInflater inflater = LayoutInflater.from(this);
inflater.inflate(R.layout.yourcustomviewxml, layout);

这篇关于如何通过的AttributeSet自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆