zoukankan      html  css  js  c++  java
  • 未解决问题

    @、

    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
  • 相关阅读:
    POJ 1987 Distance Statistics(树的点分治)
    rabbitmq-c初探
    [置顶] Android开发实战记录(三)---HelloWorld
    debug连线指令
    Qt之信号连接,你Out了吗?
    hdu-4607-Park Visit
    MySQL 分区表 partition线上修改分区字段,后续进一步学习partition (1)
    如何用正则将多个空格看成一个空格结合spllit()方法将文本数据入库
    hdu 1711 Number Sequence(KMP模板题)
    [leetcode]Unique Paths
  • 原文地址:https://www.cnblogs.com/yarightok/p/5850232.html
Copyright © 2011-2022 走看看