我需要我自己的具有复杂定制设计的 RadioButton(仅使用九个补丁是不可能的)。
所以我坚持要扩展 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。
谢谢你帮我解决这个问题。
最好的问候, 马可