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
  • 相关阅读:
    修改浏览器的UserAgent来伪装你的浏览器和操作系统
    探索并发编程(七)分布式环境中并发问题
    通信网络规划的最短路径(最小生成树的2种算法介绍)
    iOS 面向模型的 SQL存储
    一个有趣的天平秤球问题
    飞机加油问题的粗略探究
    粗谈设计模式
    JDK动态代理[2]JDK动态代理的底层实现之Proxy源码分析
    Java集合系列[2]LinkedList源码分析
    JDK动态代理[4]ProxyGenerator生成代理类的字节码文件解析
  • 原文地址:https://www.cnblogs.com/yarightok/p/5850232.html
Copyright © 2011-2022 走看看