【问题标题】:How to pass AttributeSet to custom View如何将 AttributeSet 传递给自定义视图
【发布时间】:2011-04-11 22:24:02
【问题描述】:

如何将当前的 AttributeSet 传递给自定义 View 类?如果我使用仅在参数中包含 Context 的构造函数,我将失去所有主题以及在该自定义视图的 xml 中使用“样式”标签的能力。

我所做的是在 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:

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

自定义视图类:

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:

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

【问题讨论】:

    标签: android


    【解决方案1】:

    而不是用 layout.addView(new CustomLayout1(getApplicationContext()));使用 Activity 中的 LayoutInflater 为它充气。

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

    【讨论】:

    • 简单。优雅的。谢谢你。我自己应该想到的 :-P 这是一个非常烦人的行为,我希望看到它得到修复。我已经在我的主代码中实现了该解决方案,当然还发现了许多其他相关问题。仅仅因为您调用自定义视图的方式,主题和样式就会如此神秘地消失,这似乎是一个错误。 叹息
    【解决方案2】:

    您的代码在您的自定义视图的线性布局内创建 LinearLayout。这样做的正确方法是将您的自定义视图 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>
    

    <merge xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView android:layout_width="wrap_content"
          android:layout_height="wrap_content" 
          android:text="Oh, Hewroh..."
          style="?textview_style1" 
    />
    </merge>
    
    【解决方案3】:

    你想在这里完成什么?看起来您在这里使用您的 create 方法有一个无限递归循环,因为 inflate() 将调用带有属性的构造函数。无论如何,要回答您的问题,您会在带有属性的构造函数中获得属性!

    这是从 XML 加载时调用的构造函数,否则它会调用您提供的其他构造函数之一。

    另一件有用的事情是,您可以从静态 View 方法中更轻松地获得对充气机的引用。查看.inflate :D

    【讨论】:

    • 这是一个测试类,用于解决我在另一个项目中遇到的问题。我正在创建自定义视图并将它们添加到图库视图中。我遇到的问题是我无法将我正在使用的主题应用于自定义类中的任何内容。这似乎是变态和错误的:-P 我的问题是如何让活动中的属性集传递给我的自定义类?我目前正在这样做: new CustomLayout1(getApplicationContext()) 而我想做这样的事情: new CustomLayout1(getApplicationContext(), AttributeSet) 希望它能真正应用我的主题
    • 你在构造函数中获取它们,只需创建一个 AttributeSet 成员 mAttset = attrs
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-16
    • 2015-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多