【问题标题】:How to inflate Android View in LinearLayout class?如何在 LinearLayout 类中为 Android View 充气?
【发布时间】:2013-10-07 18:36:57
【问题描述】:

我有一小段 xml,我将在我的应用程序的很多地方使用它。出于这个原因,我想将它存储在一个单独的文件中。所以我创建了 mywidget.xml,其中有我的 xml。然后我尝试在 mywidget.java 中扩展它,之后我想将它包含在不同的 xml 文件中,如下所示:

<com.mycom.android.ui.widget.AmountWidget android:layout_width="fill_parent" android:layout_height="wrap_content"></com.mycom.android.ui.widget.AmountWidget>

在我的 java 文件中,我尝试像这样膨胀初始 xml:

public class AmountWidget extends LinearLayout {
    public AmountWidget(Context context) {
        super(context);
        LinearLayout ll = (LinearLayout) findViewById(R.layout.amount_widget);
        addView(ll);
    }
}

但是使用上面的代码时,我收到一条错误消息,指出扩展类 com.mycom.android.ui.widget.AmountWiget 时出错。

我的问题:有谁知道我可以如何扩展布局,以便我可以将其用作另一个 xml 布局文件中的类?

小部件中的 xml 如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:layout_margin="10dp"
    android:padding="10dp"
    android:background="@layout/border"
    >
    <EditText
        android:id="@+id/payment_amount_major"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textSize="35sp"
        android:textStyle="bold"
        android:inputType="number"
        android:digits="0,1,2,3,4,5,6,7,8,9"
        android:maxLength="9"  
        android:gravity="right"
        />
</LinearLayout>

【问题讨论】:

标签: java android xml android-layout


【解决方案1】:

试试这个:

mContainerView = (LinearLayout)findViewById(R.id.parentView);    
LayoutInflater inflater =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
View myView = inflater.inflate(R.layout.row, null);  
mContainerView.addView(myView); 

mContainerViewis LinearLayout 其中包含您的EditTextrow 是您的xml 文件名。

【讨论】:

  • 嗨@ved - 我尝试了你的建议,但我收到一条消息说“方法 getSystemService(String) 未定义 AmountWidget 类型”。有什么想法可以解决这个问题
【解决方案2】:

View 类有一个 inflate method 包装 LayoutInflater.inflate。您应该可以使用:

LinearLayout ll = (LinearLayout) inflate(context, R.layout.amount_widget, this);

从 xml 扩展您的小部件。不需要调用addView(),因为 inflate 将为您添加新的膨胀视图!

编辑:请注意,因为这个视图已经是一个线性布局,所以没有必要让你膨胀的 xml 的根也是一个线性布局。如果您只膨胀 EditText 并将其添加到父级,而不是在父级中嵌套第二个 LinearLayout,它可以提高您的性能。您可以直接在 AmountWidget 上设置 LinearLayout 属性(例如背景和填充),无论它添加到 xml 中。在这种特定情况下,这应该无关紧要,但如果您遇到具有许多嵌套视图的情况,可能会很高兴知道。

Edit2:View 类具有 three constructors:View(Context)、View(Context, AttributeSet) 和 View(Context, AttributeSet, int)。当系统从 xml 扩展视图时,它将使用后两者之一。任何自定义视图都需要实现所有这三个构造函数。在重用代码的同时,一个简单的方法是这样的:

public AmountWidget(Context context) {
    super(context);
    LinearLayout ll = (LinearLayout) inflate(context, R.layout.amount_widget, this);
}

public AmountWidget(Context context, AttributeSet attrs) {
    this(context);
}

public AmountWidget(Context context, AttributeSet attrs, int defStyle) {
    this(context);
}

如果您不关心属性或样式参数是什么,并且只希望 AmountWidget 在其膨胀时创建相同,这将起作用。

【讨论】:

  • 嗨@MattDavis - 我尝试了你的建议,但我得到了相同的 android.view.InflatException: Error inflating class com.mycom.android.ui.widget.AmountWidget。知道可能出了什么问题吗?
  • 您是否通过在另一个 xml 文件中引用它来使用此视图?您可能需要覆盖所有三个 View 构造函数,因为系统将尝试使用一个带有更多参数的构造函数。我会用一个例子来编辑我的帖子。
  • 好吧,这让我更进一步;我现在有一个错误说“特定的孩子已经有一个父母,我需要先在孩子的父母上调用 removeView()”。尽管它似乎准确地说明了我需要做什么,但我有点迷失了。这是因为你所说的关于我的 LinearView 在新的 LinearView 中膨胀的事情,还是因为其他原因?
  • 尝试移除对addView()的调用,inflate 可能会附加膨胀的视图。如果这不起作用,请尝试传递 null 而不是 this 作为 inflate 的第三个参数,而不是删除 addView()
  • 太棒了!删除 addView() 解决了一切!先生,你让我开心!来自阿姆斯特丹的问候!
【解决方案3】:

最简单的解决方案

LinearLayout item = (LinearLayout )findViewById(R.id.item);//where you want to add/inflate a view as a child

View child = getLayoutInflater().inflate(R.layout.child, null);//child.xml

item.addView(child);

ImageView Imgitem = (ImageView ) child.findViewById(R.id.item_img);

Imgitem.setOnClick(new ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-13
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-15
    相关资源
    最近更新 更多