我发现了很多关于如何制作没有文字的自定义ProgressDialog的教程。使用自定义图像和消息创建自定义ProgressDialog的最简单方法是什么?事情是这样的......Android自定义ProgressDialog与消息
回答
创建自定义对话框
如果你想为一个对话框,以定制的设计,您可以用布局和widget元素的对话窗口中创建自己的布局。在定义布局之后,将根视图对象或布局资源ID传递给setContentView(View)。
例如,创建右侧所示的对话框:
创建保存为custom_dialog.xml的XML布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
>
<ImageView android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp"
/>
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF"
/>
</LinearLayout>
此XML定义了一个ImageView的和的LinearLayout内一个TextView。 设置上述布局对话框的内容视图并定义的ImageView和TextView的元素内容:
Context mContext = getApplicationContext();
Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.android);
后您实例的对话框中,设置自定义布局对话框的内容视图用的setContentView(INT),传递它是布局资源ID。既然对话框具有已定义的布局,您可以使用findViewById(int)从布局中捕获视图对象并修改其内容。 就是这样。您现在可以按照显示对话框中所述显示对话框。 使用基础Dialog类进行的对话框必须有标题。如果您不调用setTitle(),则用于标题的空间保持空白,但仍然可见。如果您根本不需要标题,那么您应该使用AlertDialog类创建自定义对话框。但是,由于AlertDialog是使用AlertDialog.Builder类创建的最简单的方法,因此无法访问上面使用的setContentView(int)方法。相反,你必须使用setView(View)。该方法接受一个View对象,所以你需要从XML中扩展布局的根视图对象。
要扩充XML布局,请使用getLayoutInflater()(或getSystemService())检索LayoutInflater,然后调用inflate(int,ViewGroup),其中第一个参数是布局资源ID,第二个参数是ID根视图。此时,您可以使用膨胀的布局在布局中查找View对象,并为ImageView和TextView元素定义内容。然后实例化AlertDialog.Builder并使用setView(View)为对话框设置充气布局。
下面是一个例子,在AlertDialog创建自定义布局:
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog,
(ViewGroup) findViewById(R.id.layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
使用一个AlertDialog您的自定义布局使您可以利用的优势内置AlertDialog功能,比如管理按钮,选择列表,一个标题,一个图标等等。
有关更多信息,请参阅Dialog和AlertDialog.Builder类的参考文档。
- 1. 开发自定义progressdialog android
- 2. 使用自定义ProgressDialog android
- 3. 如何自定义Android ProgressDialog?
- 4. Android自定义ProgressDialog动画
- 5. 改变ProgressDialog的消息android
- 6. 自定义ProgressDialog
- 7. Upadate progressdialog消息
- 8. android ProgressDialog:设置自定义视图而不是消息 - 这是否工作?
- 9. Android调用自定义ProgressDialog - 如何?
- 10. 如何在android中自定义ProgressDialog?
- 11. JSR-303验证与自定义消息
- 12. 自定义消息和默认消息
- 13. 自定义python异常与自定义消息
- 14. Django的allauth自定义消息:与HTML/CSS样式消息
- 15. 的WebView与自定义错误消息的Android
- 16. 自定义弹出消息
- 17. Validates_overlap自定义消息?
- 18. CodeIgniter自定义404消息
- 19. 自定义验证消息
- 20. CakePHP - 自定义消息
- 21. InvalidConstraintException的自定义消息
- 22. 自定义消息(JSF)
- 23. 自定义验证消息
- 24. 自定义错误消息
- 25. JSF 2.0自定义消息
- 26. 自定义S3 403消息
- 27. Tablix行自定义消息
- 28. WPF:自定义消息框
- 29. 自定义消息框WPF
- 30. Win32自定义消息框
感谢您对我的搜索引擎优化,我会在角落里坐几分钟的时间来处罚。 –