複雑なカスタム デザインを備えた独自の RadioButton が必要です (これは 9 パッチのみでは不可能です)。
そこで、RadioButton を拡張し、RadioGroup のすべての機能を使用することにしました。
複雑なカスタム デザインでは、LinearLayout をインフレートします (最初は RelativeLayout を使用しましたが、NullPointerException をスローするように見えるため、代わりに動作する LinearLayout を試しました)。
さて、LinearLayout の測定、レイアウト、および描画にいくつか問題があります。
この複雑なカスタム デザインがあるとします。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="100dip" android:layout_height="wrap_content" android:orientation="vertical" android:layout_weight="1"... >
<TextView android:id="@+id/tab_count" android:layout_width="wrap_content" android:layout_height="wrap_content"... />
<TextView android:id="@+id/tab_text" android:layout_width="wrap_content" android:layout_height="wrap_content"... />
</LinearLayout>
そして、これは「新しい」ビューです:
public class RadioView extends RadioButton {
private LinearLayout container;
private TextView count, text;
public RadioView(Context context) {
super(context);
init(context);
}
public RadioView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public RadioView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(final Context context) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
container = (LinearLayout) inflater.inflate(R.layout.ve_radio, null);
count = (TextView) container.findViewById(R.id.tab_count);
text = (TextView) container.findViewById(R.id.tab_text);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
container.measure(widthMeasureSpec, heightMeasureSpec);
// here comes the problem
setMeasuredDimension(200, 200);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
container.layout(left, top, right, bottom);
}
@Override
protected void onDraw(Canvas canvas) {
container.draw(canvas);
}
}
実際、測定寸法の計算方法がわかりません。正直に言うと、RadioGroup に十分なスペースを計算してもらい、このスペースを膨張した LinearLayout に委譲したいと考えています。
これについて私を助けてくれてありがとう。
よろしく、マルコ