10

アプリの多くの場所で使用する xml の小さな部分があります。このため、別のファイルに保存したいと思います。そこで、xml を含む mywidget.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>
4

3 に答える 3

14

これを試して:

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

mContainerViewEditTextあなたを含むLinearLayoutでありrow、xmlファイル名です。

于 2013-10-01T14:40:21.537 に答える