代码之家  ›  专栏  ›  技术社区  ›  Binod Singh

自定义视图扩展相对布局

  •  21
  • Binod Singh  · 技术社区  · 10 年前
    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>
    

    刚刚开始添加一个新的自定义视图,并得到一次错误。如果我清除了此项,则可以继续

    我正在崩溃“由:android.view.InflateException:二进制XML文件行#1:类膨胀错误”

    2 回复  |  直到 10 年前
        1
  •  44
  •   Community Mike Kinghan    7 年前

    您还需要有两个构造函数。了解原因

    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>
    

    主要活动.java

    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            }
    }
    

    断裂

    enter image description here

    正如pskink在 activity_main.xml 具有子CustomView。然后CustomView扩展RealiveLayout,然后再次使用RelativeLayout和子TextView扩展自定义视图。不需要所有这些。只是一个自定义视图。以编程方式创建文本视图,然后将文本视图添加到RelativeLayout

    编辑:

    活动_最小.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");
       }
    }
    
        2
  •  0
  •   Yuvaraja    10 年前

    尝试获取“活动”并使用此

     {
        LayoutInflater inflter = activity.getLayoutInflater();
        View v = inflter.inflate(R.layout.custom_view,null);
        this.addView(v); or addView(v);
        }