Java 如何在 LinearLayout 类中膨胀 Android 视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19118943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:16:41  来源:igfitidea点击:

How to inflate Android View in LinearLayout class?

javaandroidxmlandroid-layout

提问by kramer65

I've got a little piece of xml, which I'll be using in a lot of places in my app. For this reason I want to store it in a separate file. So I created mywidget.xml in which I have my xml. I then try to inflate this in mywidget.java, after which I want to include it in a different xml file like so:

我有一小段 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>

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

In my java file, I try to inflate the initial xml like this:

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

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

But with the code above I get an error saying that there's an error inflating class com.mycom.android.ui.widget.AmountWiget.

但是在上面的代码中,我收到一个错误消息,指出在增加类 com.mycom.android.ui.widget.AmountWiget 时出现错误。

My question: Does anybody know how I can inflate a layout so that I can use it as a class in another xml layout file?

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

The xml from the widget looks like this:

来自小部件的 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>

采纳答案by MattDavis

The Viewclass has an inflate methodwhich wraps LayoutInflater.inflate. You should be able to use:

View类有一个充气方法,其包装LayoutInflater.inflate。您应该能够使用:

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

to inflate your widget from xml. The call to addView()won't be needed, as inflate will add the newly inflated view for you!

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

Edit: Just a note, because this View is already a LinearLayout, there's no need to have the root of the xml you're inflating also be a LinearLayout. It can increase your performance if you inflate only the EditText and just add that to the parent, rather than nesting a second LinearLayout within the parent. You can set the LinearLayout attributes (such as background and padding) directly on the AmountWidgetwherever it's added in xml. This shouldn't matter too much in this specific case, but may be good to know going forward if you have a situation with many nested views.

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

Edit2: The Viewclass has three constructors: View(Context), View(Context, AttributeSet), and View(Context, AttributeSet, int). When the system inflates a view from xml, it will use one of the latter two. Any custom View will need to implement all three of these constructors. An easy way to do this while reusing your code is like this:

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

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

This will work if you don't care what the attributes or style arguments are, and just want the AmountWidget created the same any time it's inflated.

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

回答by ved

Try this:

尝试这个:

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 which contain your EditTextand rowis your xml filename.

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

回答by Jawad Zeb

Easiest Solution

最简单的解决方案

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