使用 Typedarray returns 的自定义视图 null 所有参数

Custom view with Typedarray returns null all parameters

我必须创建自定义线性布局以供重复使用。线性布局有imageview和textview。

我关注当前 creating custom layout

请找到我的代码

MainActivity.java

    package com.customview.compoundview;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Toast;
import com.customview.compoundview.R;

public class MainActivity extends ActionBarActivity {

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

    public void onClicked(View view) {
        String text = view.getId() == R.id.view1 ? "Background" : "Foreground";
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
      }



}

ColorOptionsView.java

/**
 * 
 */
package com.customview.compoundview;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.customview.compoundview.R;

/**
 * @author gaurav
 * Feb 18, 2015
 */
public class ColorOptionsView extends LinearLayout {

    ImageView imageButton_icon;
    TextView textView_caption;

    public ColorOptionsView(Context context) {
        super(context);
    }

    public ColorOptionsView(Context context, AttributeSet attrs) {
        super(context, attrs);
        Log.d("ColorOptionsView",""+1);
        LayoutInflater inflater = LayoutInflater.from(context);
        Log.d("ColorOptionsView",""+2);
        inflater.inflate(R.layout.view_color_options, this);
        Log.d("ColorOptionsView",""+3);

    imageButton_icon = (ImageView) findViewById(R.id.imageView);
    textView_caption = (TextView) findViewById(R.id.textView_caption);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CaptionButton);
        Log.d("ColorOptionsView",""+4);
        Drawable icon;
        String caption;
        try{
            icon = a.getDrawable(R.styleable.CaptionButton_button_icon);
            Log.d("ColorOptionsView",""+5);
            caption = a.getString(R.styleable.CaptionButton_caption);
            Log.d("ColorOptionsView",""+6 +caption );
        }
        finally
        {
            a.recycle();
        }

        setIcon(icon);
        setCaption(caption);
    }

    public void setIcon(Drawable icon) {
        try{
            Log.d("ColorOptionsView",""+7 +icon );
            imageButton_icon.setImageDrawable(icon);
        }
        finally{
            System.out.println("error in seticon "+icon);
        }
    }

    public void setCaption(String caption) {
        try{
        Log.d("ColorOptionsView",""+8 +caption );
        textView_caption.setText(caption);
        }
        finally
        {
            System.out.println("error in setCaption"+caption);
        }
    }
}

fragment_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:myapp="http://schemas.android.com/apk/lib/com.vogella.android.view.compoundview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:showDividers="middle"
    android:divider="?android:attr/listDivider"
    tools:context=".MainActivity" >

        <com.customview.compoundview.ColorOptionsView

            android:id="@+id/view1"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            myapp:caption="background color"
            myapp:button_icon="@drawable/handy_man"
             />
        <TextView 
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="#f0f"
        android:text="hdffdlsakjfkdasj"
        />

        <com.customview.compoundview.ColorOptionsView

            android:id="@+id/view1"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            myapp:caption="Foreground color"
            myapp:button_icon="@drawable/history"
             />


</LinearLayout>

view_color_options.xml

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

              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/linearLayout"
              android:orientation="horizontal">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:src="@drawable/handy_man"/>

    <TextView
        android:layout_marginLeft="50dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Caption fdafasfs"
        android:id="@+id/textView_caption"
        android:gravity="center_horizontal"/>

</LinearLayout>

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="CaptionButton">
        <attr name="button_icon" format="reference" />
        <attr name="caption" format="string" />
    </declare-styleable>

</resources>

由于接收到空值,我在设置文本和图标时总是出错。这是logcat输出

02-18 17:25:26.057: E/AndroidRuntime(16164): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.customview.compoundview/com.customview.compoundview.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class com.customview.compoundview.ColorOptionsView
02-18 17:25:26.057: E/AndroidRuntime(16164): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class com.customview.compoundview.ColorOptionsView
02-18 17:25:26.057: E/AndroidRuntime(16164):    at com.customview.compoundview.ColorOptionsView.setIcon(ColorOptionsView.java:61)
02-18 17:25:26.057: E/AndroidRuntime(16164):    at com.customview.compoundview.ColorOptionsView.<init>(ColorOptionsView.java:54)

任何帮助将不胜感激。提前致谢。

现在它不会给出任何错误,但不会创建线性布局。输出如下。

ImageButton imageButton_icon;
TextView textView_caption;

从未初始化。即使您以 this 作为参数调用 inflater,您也必须调用 findViewById 来初始化这两个成员。我还注意到您正在实例化 LayoutInflaterViewGroup 有静态方法 inflate。你可以替换

  inflater.inflate(R.layout.view_color_options, this);

inflate(R.layout.view_color_options, this);