在布局 XML 中引用自定义视图时出现问题

发布于 2024-09-11 17:18:55 字数 1271 浏览 7 评论 0原文

我试图在 helloWorld XML 布局中引用自定义视图,但出现以下异常: 膨胀类 acme.my.MyTextView 时出错。

但是,我可以实例化该视图并将其手动添加到主要内容视图中。自定义视图是根据其自己的 XML 布局构建的。我该如何让它发挥作用?

public class MyTextView extends LinearLayout {  

 MyTextView(Context context){
  super(context);

  LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  layoutInflater.inflate(R.layout.my_text_view,this);
 }

 MyTextView(Context context, AttributeSet attrs){
  super(context, attrs);

  LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  layoutInflater.inflate(R.layout.my_text_view,this);
 }
}



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />

  <view class = "acme.my.MyTextView"
 android:id="@+id/myView" 
 android:orientation="vertical"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"           
 />

I am trying to refer to a custom View in the helloWorld XML layout but I get the following exception:
Error inflating class acme.my.MyTextView.

However, I am able to instantiate the view and add it to the main content view manually. The custom View is built from it's own XML layout. How do I get this to work?

public class MyTextView extends LinearLayout {  

 MyTextView(Context context){
  super(context);

  LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  layoutInflater.inflate(R.layout.my_text_view,this);
 }

 MyTextView(Context context, AttributeSet attrs){
  super(context, attrs);

  LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  layoutInflater.inflate(R.layout.my_text_view,this);
 }
}



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />

  <view class = "acme.my.MyTextView"
 android:id="@+id/myView" 
 android:orientation="vertical"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"           
 />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

呢古 2024-09-18 17:18:55

构造函数不是公开的。
:(

The constructors aren't public.
:(

洋洋洒洒 2024-09-18 17:18:55

你必须像这样调用它

 <com.example.MyLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

同时让你的构造函数公开

you have to call it like this

 <com.example.MyLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

Also make your constructors public

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文