2

これが私のカスタムビューです:

public class BuzzView extends View {

    /**
     * Constructor.  This version is only needed if you will be instantiating
     * the object manually (not from a layout XML file).
     * @param context
     */
    public BuzzView(Context context) {
        super(context);
        View.inflate(context, R.layout.buzz_view, null);
    }
}

私のbuzz_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/vignette_image_jauge"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:src="@drawable/buzzomettre_sample" >
    </ImageView>

</RelativeLayout>

アクティビティのFrameLayoutからカスタムビューを追加したい場合:

 BuzzView buzzView = new BuzzView(MosaiqueListActivity.this);
    FrameLayout frameLayout = (FrameLayout) 
v.findViewById(R.id.vignette_layout_jauge);
                frameLayout.addView(buzzView);

私のRelativeLayoutでカスタムビューを取得していません。なぜなのかご存知ですか?

4

1 に答える 1

1

このようにBuzzViewを作成してみてください

public class CustomView {

private Context mContext;
private View mCustomView;
private LayoutInflater mInflater;
public CustomView(Context context) {
    // TODO Auto-generated constructor stub
    mContext = context;
    mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public View getView() {
    if(mCustomView==null) {
        mCustomView = mInflater.inflate(R.layout.customView, null);
        //initialize all the child here
    }
    return mCustomView;
}
}

ビューを追加している間、アクティビティでこのようなgetView()メソッドを呼び出すだけです。

CustomView mCustomView = new CustomView(MyActivity.this);
layout.addView(mCustomView.getView(), new LayoutParams(LayoutParams.WRAP_CONTENT,    LayoutParams.WRAP_CONTENT));
于 2013-01-31T12:08:41.077 に答える