自定义视图扩展相对布局

21
package com.binod.customviewtest;

import android.content.Context;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;

public class CustomView extends RelativeLayout{

    public CustomView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub

//      LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LayoutInflater mInflater = LayoutInflater.from(context);
        mInflater.inflate(R.layout.custom_view  , this, true);
    }

}

包括在内
<RelativeLayout 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" >

    <com.binod.customviewtest.CustomView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ></com.binod.customviewtest.CustomView>

</RelativeLayout>

自定义视图作为

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

我刚开始添加一个新的自定义视图,在此期间遇到了错误,如果清除这个错误就可以继续前进

我遇到了崩溃问题"Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class"


你还有其他的构造函数吗?请发布自定义视图的包名以及包含自定义视图的XML。 - Raghunandan
提供 custom_view.xml 文件。 - Roman Black
@RomanBlack 在帖子中添加了自定义视图。 - Binod Singh
目前只有一个构造函数,@Raghunandan。 - Binod Singh
@binod,请现在检查我的帖子。应该已经修复了。 - Raghunandan
2个回答

44

你需要再添加两个构造函数。要知道为什么,请参考Do I need all three constructors for an Android custom view?

public class CustomView extends RelativeLayout{

    LayoutInflater mInflater;
    public CustomView(Context context) {
        super(context);
         mInflater = LayoutInflater.from(context);
         init(); 

    }
    public CustomView(Context context, AttributeSet attrs, int defStyle)
    {
    super(context, attrs, defStyle);
    mInflater = LayoutInflater.from(context);
    init(); 
    }
    public CustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
    mInflater = LayoutInflater.from(context);
    init(); 
    }
   public void init()
   {
       View v = mInflater.inflate(R.layout.custom_view, this, true);
       TextView tv = (TextView) v.findViewById(R.id.textView1);
   tv.setText(" Custom RelativeLayout");
   }
}

我正在发布一个示例。我的包名称不同。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
<com.example.testall.CustomView
        android:id="@+id/timer1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</RelativeLayout>

自定义视图.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        android:text="My Custom View" />

</RelativeLayout>

MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        }
}

Snap

图片描述

如pskink所建议的,activity_main.xml 中有一个RelativeLayout,其中包含一个CustomView。然后CustomView扩展了RelativeLayout,再次使用RelativeLayout和一个TextView作为其子项来填充CustomView。这些都不需要。只需要一个CustomView。创建一个程序化的TextView,然后将其添加到RelativeLayout中即可。

编辑:

activity_main.xml

<com.example.testall.CustomView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/timer1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

自定义视图

public class CustomView extends RelativeLayout{

    TextView tv;
    public CustomView(Context context) {
        super(context);
         tv = new TextView(context);
         init(); 

    }
    public CustomView(Context context, AttributeSet attrs, int defStyle)
    {
    super(context, attrs, defStyle);
    tv = new TextView(context);
    init(); 
    }
    public CustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
    tv = new TextView(context);
    init(); 
    }
   public void init()
   {
       this.addView(tv);
       tv.setText(" Custom RelativeLayout");
   }
}

@Raghunandan,你将会用三个RelativeLayout代替一个RelativeLayout。 - pskink
@pskink编辑了,但我想指出构造函数的问题。无论如何,你关于相对布局的观点是正确的,那是不必要的。 - Raghunandan
@binod请检查编辑。不需要3个RelativeLayout,您甚至可以对齐textview并将属性设置为相同。 - Raghunandan
第一段代码可以更新。在最终构造函数被调用之前,不要调用super,而是调用this()。在那里填充视图。在你的代码片段中,"v"从未被创建,尽管显然它应该由充气器返回,你不必保留对其的引用。这样可以消除"init"和不需要保留引用的需要。 - BajaBob
1
@BajaBob 是的,你可以这样做。我在第一次看的时候错过了这个,第二个自定义视图也可以改进。 - Raghunandan

0
尝试获取Activity并使用它。
 {
    LayoutInflater inflter = activity.getLayoutInflater();
    View v = inflter.inflate(R.layout.custom_view,null);
    this.addView(v); or addView(v);
    }

我不认为那是问题。我猜Activity Context被传递到了构造函数中。 - Raghunandan
Activity也不起作用。我在xml中包含它,如下所示: <com.binod.customviewtest.CustomView android:layout_width="match_parent" android:layout_height="wrap_content" ></com.binod.customviewtest.CustomView> - Binod Singh
@binod 请提供更多信息。您拥有自定义视图的XML以及自定义视图的包名称和任何其他构造函数。您提供的信息太少,无法解决您的问题。 - Raghunandan
@binod 编辑并发布完整的代码。注释不是添加代码(包括包名)的地方。 - Raghunandan
@Raghunandan 帖子已编辑。如果需要更多信息,请告诉我。 - Binod Singh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接