CustomView
を拡張するクラスがありますLinearLayout
。CustomElement
も拡張する別のクラスがありますLinearLayout
。クラスをXMLで使用しようとすると、何も表示されません。
これは私のクラスCustomViewです:
private static int NUMBER_OF_ELEMENTS = 4;
public CustomView(final Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
// get size for each element
int width = getWidth() / NUMBER_OF_ELEMENTS;
int height = getHeight() / NUMBER_OF_ELEMENTS;
for (int i = 0; i < NUMBER_OF_ELEMENTS; i++) {
CustomElement element = new CustomElement(context);
addView(element, width, height);
}
}
これは私のクラスCustomElementです:
public CustomElement(final Context context) {
super(context);
m_context = context;
init(context);
}
private void init(Context context) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.custom_elem, this);
}
XMLに追加しようとすると、CustomView
何も表示されません。これが私のXMLコードです:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_mainleft"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.package.views.CustomView
android:id="@+id/layout_elements"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" />
</RelativeLayout>
私は何かが足りないのですか?どんな助けでも大歓迎です!