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