我需要自己的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。
感谢您帮我解决这个问题。
最诚挚的问候, 马可
答案 0 :(得分:0)
嗯,解决方案非常简单:)
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
container.measure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(container.getMeasuredWidth(), container.getMeasuredHeight());
}
现在我想谈谈RelatriveLayout NullPointerException。
我将LinearLayout更改为RelativeLayout,这就是我得到的:
FATAL EXCEPTION: main
java.lang.NullPointerException
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:472)
at android.view.View.measure(View.java:12603)
at nu.style.RadioView.onMeasure(RadioView.java:84)
at android.view.View.measure(View.java:12603)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1196)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:555)
at android.view.View.measure(View.java:12603)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:594)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:376)
at android.view.View.measure(View.java:12603)
at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1163)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at android.widget.ScrollView.onMeasure(ScrollView.java:312)
at android.view.View.measure(View.java:12603)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:594)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:376)
at android.view.View.measure(View.java:12603)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4677)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at android.view.View.measure(View.java:12603)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4677)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
at android.view.View.measure(View.java:12603)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4677)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2072)
at android.view.View.measure(View.java:12603)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1044)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2418)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4340)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
感谢您的帮助。
祝你好运, 马可