java - Android - 自定义对话框

标签 java android dialog android-custom-view

我正在尝试创建一个自定义对话框,其中对话框内的内容是动态的。所以我将一个 View 传递给对话框,它会将该 View 添加到对话框中。目前,它不绘制自定义 View ,并表示该 View 的宽度和高度为 0。

我通过 Application 类传递自定义 View 以及一系列其他自定义项。

这是我的对话类:

    public class LoadingActivity extends Dialog
    {
        private LoadingActivity(Context a) {
            super(a, android.R.style.Theme);
        }

        public static LoadingActivity show(Context context) {

            LoadingActivity checkerLoader = new LoadingActivity(context);

            View currentView = View.inflate(context, R.layout.loading_activity, null);
            FrameLayout linearLayout = (FrameLayout) currentView.findViewById(R.id.circular_progress_bar);
            RelativeLayout viewLayout = (RelativeLayout) currentView.findViewById(R.id.view_layout);

            if (DataManager.getInstance().getAppConfigurations().getLoadingActivity() != null) {
                View loadingView = DataManager.getInstance().getAppConfigurations().getLoadingActivity();
                loadingView.setBackgroundColor(Color.RED);
                loadingView.invalidate();
                if(loadingView.getParent()!=null) {
                    ((ViewGroup) loadingView.getParent()).removeView(loadingView);
                }
                viewLayout.addView(loadingView);
            }

            checkerLoader.requestWindowFeature(Window.FEATURE_NO_TITLE); //before
            checkerLoader.setContentView(currentView);

            checkerLoader.setCancelable(false);
            checkerLoader.getWindow().getAttributes().gravity = Gravity.CENTER;

            WindowManager.LayoutParams lp = checkerLoader.getWindow().getAttributes();
            lp.dimAmount=0.2f;

            checkerLoader.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
            checkerLoader.getWindow().setAttributes(lp);
            checkerLoader.show();

            return checkerLoader;
        }
    }

这是我的自定义 View 的示例:

    public class Spinner extends LinearLayout
    {
        public Spinner(Context context) {
            super(context);
            init(context);
        }

        public Spinner(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
            init(context);
        }

        public Spinner(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init(context);
        }

        private void init(Context context) {

            View aView = View.inflate(context, R.layout.loading_activity, null);
            ImageView logoImage = (ImageView) aView.findViewById(R.id.loading_image);
            logoImage.setImageResource(R.drawable.loading_chicken2);
        }
    }

最佳答案

自定义 View 微调器不绘制任何内容,因为膨胀的布局“R.layout.loading_activity”未添加到父级。

改变

 View aView = View.inflate(context, R.layout.loading_activity, null);

到:

 View aView = View.inflate(context, R.layout.loading_activity, this);

可能有帮助

关于java - Android - 自定义对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46935759/

相关文章:

java - 如何将字体设置为 AlertDialog.Builder 的 setMessage 部分?

javascript - 链接页面的 HTML/JS 解释对话框

c++ - 我可以在调整大小时锁定 MFC 对话框的宽度或高度吗?

c++ - 如何使用 WTL 或 Windows SDK(无 MFC)创建 TitleAreaDialog?

java - 多线程的启动和加入顺序不同?

Java代理连接到postgres

android - 添加 Drawable 小部件后,mainactivity 中的 ListView 不可滚动

java - View arg0 和 View v 有什么区别?

java - 如何使用itextpdf生成pdf文件时创建页脚

java - 可空元素的二维非空数组