Android 自定义视图扩展相对布局
包括作为Android 自定义视图扩展相对布局,android,custom-view,Android,Custom View,包括作为 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
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>
刚开始添加一个新的自定义视图,并得到了一次错误,如果我清除这个,然后可以继续
“我正在崩溃”是由以下原因引起的:android.view.InflateException:二进制XML文件行#1:膨胀类时出错“尝试获取活动并使用此
<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>
你需要有两个以上的构造函数。知道为什么吗 我举了一个例子。我的名字不一样
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");
}
}
折断
正如pskink在activity_main.xml
中的RelativeLayout中所建议的,它有一个子CustomView。然后CustomView扩展RealtiveLayout,然后再次使用RelativeLayout和子TextView膨胀CustomView。不需要所有这些。只是一个自定义视图。以编程方式创建TextView,然后将TextView添加到RelativeLayout
编辑:
activity_main.xml
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
您是否也有其他构造函数,并发布customview的包名和包含customview的xml提供custom_view.xmlfile@RomanBlack自定义视图添加到post@Raghunandan截至,没有唯一的构造函数now@binod现在查看我的帖子。我认为这不是问题所在。活动上下文被传递给构造函数,我猜活动也不起作用。我将此包含在xml中@binod,请发布更多信息。在其中包含customview和packagename for customview的xml以及您拥有的任何其他客户结构。您提供的信息很少,无法解决您的问题problem@binod编辑并发布完整的代码注释不是添加代码(包括软件包)的地方name@Raghunandan这篇文章是经过编辑的。如果还有什么需要,请告诉我needed@Raghunandan而不是一个亲戚你会得到三个RelativeLayouts@pskink编辑,但我想指出关于构造函数。不管你对相对论的看法如何,这都是不必要的。@b请检查编辑。无需3个相对性窗口,您甚至可以将textview和设置属性对齐到相同的位置。第一段代码可以更新。在调用最终构造函数之前,不要调用super,而是调用this()。把那里的景色放大。“v”永远不会在代码段中创建,尽管很明显,它会由您不必要地持有引用的充气机返回。不需要“init”和保存不需要的引用。@BajaBob是的,你可以这样做。我在第一个和第二个customview中忽略了这一点
<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 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>
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
<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");
}
}