首页 > 代码库 > 未解决问题
未解决问题
@、
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(); }}
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)); } }
<?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>
未解决问题
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。