首页 > 代码库 > 未解决问题

未解决问题

@、

1、自定义了一个RelativeLayout类MyCustomLayout,然后在onDraw(...)中,画一个圆canvas.drawCircle(...)。

2、自定义了一个Dialog类MyDialog,其中xml中是RelativeLayout,并且layout_height和layout_width都是match_parent,然后在onCreate(...)中,创建一个MyCustomLayout。

3、在MainActivity中创建MyDialog,然后show。

问题:

1、为什么在MyCustomLayout的构造函数中调用了setBackgroundColor(...),画的圆才能显示。

2、为什么在MyDialog.onCreate()中创建MyCustomLayout变量的LayoutParams不能设置为wrap_conent。

代码:

技术分享
public class MyCustomLayout extends RelativeLayout {    private static final String TAG = "My--CustomView";    private int mRadius = 5;    public MyCustomLayout(Context context) {        super(context);        Log.d(TAG, "Constructor1");        // Why have to call this ??        setBackgroundColor(getResources().getColor(                android.R.color.transparent));    }    public MyCustomLayout(Context context, AttributeSet attrs) {        super(context, attrs);        Log.d(TAG, "Constructor2");        setBackgroundColor(getResources().getColor(                android.R.color.transparent));    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);//        canvas.drawColor(Color.parseColor("#000000"));        Paint paint = new Paint();        paint.setAntiAlias(true);        paint.setColor(Color.parseColor("#FFFF00"));        Log.d(TAG, getWidth() + " -- " + getHeight() + " -- " + Utils.dpToPx(mRadius, getResources()));        canvas.drawCircle(getWidth() / 2, getHeight() / 2, Utils.dpToPx(mRadius, getResources()), paint);    }    public int getRadius() {        return mRadius;    }    public void setRadius(int radius) {        mRadius = radius;        invalidate();    }}
MyCustomLayout.java
技术分享
class MyDialog extends Dialog {       MyCustomLayout mMyCustomLayout;        public MyDialog(Context context) {            super(context, android.R.style.Theme_Translucent);        }        @Override        public void show() {            super.show();            Log.d("MyDialog", "show");        }        @Override        public void dismiss() {            super.dismiss();            Log.d("MyDialog", "dismiss");//            mMyCustomView.setRadius(0);        }        @Override        protected void onCreate(Bundle savedInstanceState) {            requestWindowFeature(Window.FEATURE_NO_TITLE);            super.onCreate(savedInstanceState);            setContentView(R.layout.my_dialog);            Log.d("MyDialog", "onCreate");            RelativeLayout content = (RelativeLayout) this                    .findViewById(R.id.my_custom_view_content);            mMyCustomLayout = new MyCustomLayout(this.getContext());            content.addView(mMyCustomLayout);            mMyCustomLayout.setLayoutParams(                    new RelativeLayout.LayoutParams(                            RelativeLayout.LayoutParams.FILL_PARENT,                    RelativeLayout.LayoutParams.FILL_PARENT));        }    }
MyDialog.java
技术分享
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"                android:id="@+id/my_custom_view_content"                android:layout_width="match_parent"                android:layout_height="match_parent"> </RelativeLayout>
my_dialog.xml

 

未解决问题