私はこれに似たXMLレイアウトを持っています(XMLとコードははるかに複雑ですが、簡潔にするために簡略化しています):
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageButton
android:id="@+id/button01"
android:src="@drawable/key_01" />
<ImageButton
android:id="@+id/button02"
android:src="@drawable/key_02" />
<ImageButton
android:id="@+id/button03"
android:src="@drawable/key_03" />
</TableRow>
</TableLayout>
</merge>
私はこのレイアウトをカスタムウィジェットとして持っています。
public class ThreeButtons extends LinearLayout
{
public ThreeButtons(Context context, AttributeSet attrs)
{
super(context, attrs);
final LayoutInflater inflator = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflator.inflate(R.layout.three_buttons, this);
if (!isInEditMode())
setClickListeners();
}
...
private void setClickListeners()
{
...
}
}
問題は、私がそこにあるべきではないと思う余分なレベルの深さを持っているということです。つまり、実際のThreeButtonsクラスはLinearLayoutであり、子は1つだけです。これは、拡張されたXMLです。
ウィジェットを子として追加するよりも、XMLから直接作成する方が理にかなっていると思いました。私がやっている方法は、カスタムウィジェットを持つ唯一の方法ですか?または、よりクリーンな方法はありますか?
ありがとう、
ブラッド