android - 自定义 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(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>

自定义 View 为

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

刚开始添加一个新的自定义 View 并得到一次错误如果我清除它然后可以继续前进

我遇到崩溃“原因:android.view.InflateException: Binary XML file line #1: Error inflating class”

最佳答案

您还需要有 2 个构造函数。想知道为什么

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>

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

捕捉

enter image description here

正如pskink 在activity_main.xml 中的RelativeLayout 中建议的那样,带有一个子CustomView。然后 CustomView 扩展 RealtiveLayout,然后再次使用 RelativeLayout 和子 TextView 为 customview 充气。不需要所有这些。只是一个自定义 View 。以编程方式创建一个 TextView,然后将 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"
    />

自定义 View

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

关于android - 自定义 View 扩展相对布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22779422/

相关文章:

android - 如何添加操作栏

android - 我的 Sprite 如何从 andEngine 中的当前位置旋转(平滑旋转)给定角度?

android - 如何将按钮添加到自定义滚动和缩放 SurfaceView?

objective-c - 将 "NSView"绘制到自定义 View - 如何?我采取的方法正确吗?

android - OSMDROID android 4.2 : Mapquest: As of july 11, 2016 direct tile access 已停产

android - 启动新的 Android Activity 太慢了

java - 如何在 android Activity 中处理包含两种不同语言的编辑文本的布局的软键盘切换语言?

ios - Xcode 6. 从 nib 文件创建自定义 View

java - 从 MainActivity 调用具有参数上下文和属性集的方法

ios - 以编程方式创建的自定义 UITextField 不会在点击时调出键盘