如何在LinearLayout类中充气Android View?

kra*_*r65 9 java xml android android-layout

我有一小段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);
    }
}
Run Code Online (Sandbox Code Playgroud)

但是使用上面的代码我得到一个错误,说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>
Run Code Online (Sandbox Code Playgroud)

ved*_*ved 12

试试这个:

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); 
Run Code Online (Sandbox Code Playgroud)

mContainerView是LinearLayout,它包含你的EditTextrow你的xml文件名.


Mat*_*vis 6

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

LinearLayout ll = (LinearLayout) inflate(context, R.layout.amount_widget, this);
Run Code Online (Sandbox Code Playgroud)

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

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

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);
}
Run Code Online (Sandbox Code Playgroud)

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


Xar*_*mer 5

最简单的解决方案

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 ...
Run Code Online (Sandbox Code Playgroud)