【问题标题】:Custom View not inflating自定义视图不膨胀
【发布时间】:2012-05-16 15:33:46
【问题描述】:

我正在尝试通过 xml 创建我的自定义视图,但屏幕没有显示我的视图,因为它没有膨胀。我的自定义视图 xml 为:

 <?xml version="1.0" encoding="utf-8"?>
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/profileSwitcher"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/imgAdvertise"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:src="@android:drawable/btn_minus" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal" >

        <TextView
            android:id="@+id/txtAdvertise"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="asdas" />
    </LinearLayout>

</ViewSwitcher>

我的自定义 View 类为:

public class MyCustomView extends View{

    TextView textAdvertise;
    ImageView imageAdvertise;
    View view;
    ViewSwitcher switcher ;

    public MyCustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub

        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (layoutInflater != null) {
            view = layoutInflater.inflate(R.layout.main, null);

        }

        initialize();
    }

    public void initialize() {
        // TODO Auto-generated method stub

        textAdvertise = (TextView) view.findViewById(R.id.txtAdvertise);
        imageAdvertise = (ImageView) view.findViewById(R.id.imgAdvertise);
        switcher = (ViewSwitcher) view.findViewById(R.id.profileSwitcher);
        startAnimation();

    }

    public void startAnimation() {
        // TODO Auto-generated method stub

        new Thread() {
            public void run() {

                for (;;) {
                    try {

                        Thread.sleep(2000);
                        hRefresh.sendEmptyMessage(5);
                    } catch (Exception e) {
                    }
                }
            }
        }.start();
    }

    Handler hRefresh = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case 5:
                switcher.showNext();
                // To go back to the first view, use switcher.showPrevious()
                break;
            default:
                break;
            }
        }
    };

}

在我必须显示View 的基本xml 布局中,是一个按钮,而我的View 为:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:text="Button" 
    android:layout_weight="1"/>

<com.example.viewswitcher.MyCustomView 
    android:id="@+id/MyView"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"

    />

谁能告诉我这是怎么回事?

【问题讨论】:

    标签: android custom-controls custom-component


    【解决方案1】:

    您将看不到任何内容,因为MyCustomView 在您编写时没有任何内容。首先,我会使用其中一个ViewGroups 子代而不是View(如LinearLayoutRelativeLayout 等):

    public class MyCustomView extends LinearLayout {
    
        TextView textAdvertise;
        ImageView imageAdvertise;
        View view;
        ViewSwitcher switcher ;
    
        public MyCustomView(Context context, AttributeSet attrs) {
            super(context);
        }
    
        public MyCustomView(Context context, AttributeSet attrs) {
            super(context, attrs);
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if (layoutInflater != null) {
                view = layoutInflater.inflate(R.layout.main, this, true);
            }
            initialize();
        }
    
         public MyCustomView(Context context, AttributeSet attrs, int theme) {
             super(context, attrs, theme);
         }
    // ...
    

    【讨论】:

    • 嘿,谢谢您的帮助.. 它工作.. 实际上在我的布局充气机中,我写了 view = layoutInflater.inflate(R.layout.main, null);这个 null 会导致问题.. 而不是我把“这个”它工作了.. 谢谢哥们!
    • 你做了什么编辑有问题......还有一件事它起作用了,因为我扩展了LinearLayout而不是视图......为什么会这样?有关此详细信息的任何链接或帖子..谢谢
    • @Rashmi 我已经编辑了您的问题以更好地格式化问题的代码。 View 类表示单个 View 对象,它不接受子 Views,就像您尝试使用 inflate 方法添加到它一样(实际上,如果您查看 inflate 方法docs 你会看到第二个参数是ViewGroup)。相反,ViewGroup(LinearLayoutViewGroup 的子类) 表示接受子类ViewsView(实际上它是超类View 的子类)。我不知道任何链接,但您可以查看 ViewViewGroup 的文档。
    • 解决方案不起作用。你必须使用这个调用:layoutInflater.inflate(R.layout.color_chooser, this),正如NullPointerException 提到的那样。
    【解决方案2】:

    使用它来扩展您的布局。 编辑

      View headerView = View.inflate(this, R.layout.layout_name, null);
    

    这将返回您的 xml 文件的父布局,然后您可以直接使用它,或者您可以通过使用找到任何其他视图

    编辑

    headerView.findViewById(R.id.view_id);
    

    希望它会起作用

    【讨论】:

      【解决方案3】:

      我认为您需要的不是自定义视图。也许您应该继承自 View 的 Fragment 类。

      查看here 了解如何实现自己的片段。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多