我正在尝试创建一个自定义对话框,其中对话框内的内容是动态的。所以我将一个 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/