次のように、LayoutInflater を使用して、View のコンストラクターで View の内部を膨張させています。
public class TrackHeader extends RelativeLayout {
public TrackHeader( Context context ) {
super( context );
final LayoutInflater inflater = getLayoutInflater( );
final RelativeLayout view = (RelativeLayout) inflater.inflate(
R.layout.trackheader, this, true );
< more here >
}
(以下も試しました)
public TrackHeader( Context context ) {
super( context );
final LayoutInflater inflater = getLayoutInflater( );
final RelativeLayout view = (RelativeLayout) inflater.inflate(
R.layout.trackheader, this, false );
addView( view, new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT ) );
< more here >
}
}
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"
>
<TextView android:id="@+id/trackNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_gravity="center_horizontal"
android:textSize="16dip"
/>
<Button android:id="@+id/waveformBtn"
android:layout_width="match_parent"
android:layout_height="40dip"
android:layout_below="@+id/trackNumber"
android:layout_gravity="center_horizontal"
android:textSize="12dip"/>
<CheckBox android:id="@+id/enabledCheck"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/waveformBtn"
android:layout_gravity="center_horizontal"
android:textSize="12dip"/>
<CheckBox android:id="@+id/loopingCheck"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/enabledBtn"
android:layout_gravity="center_horizontal"
android:textSize="12dip"/>
</RelativeLayout>
このように膨らませている理由は、クラス (TrackHeader) がネストされた/内部クラスであり、XML でクラスを直接参照するのを妨げているように見えるからです。さて、膨らませるとレイアウトがうまくいきません。最上位ノード (RelativeLayout) は、TrackHeader を埋めるために正しくサイズ変更されますが、子ビューはまったくサイズ変更されず、(0, 0, 0, 0) のままです。そこにいるAndroid愛好家が私を正しい方向に向けることができるかどうか興味があります...
事前に感謝し、コーディングをお楽しみください! -E.