我在对话框中使用自定义布局来显示进度条和文本。布局看起来像
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/footer_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/popuplayout"
android:padding="8dp">
<LinearLayout android:id="@+id/progressdialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true">
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="center_vertical"
android:layout_toLeftOf="@+id/textView1" />
<TextView
android:id="@+id/progressmsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:textStyle="bold"
android:textSize="15dp"
android:padding="5dp"
android:text="loading..." />
</LinearLayout>
</RelativeLayout>
并通过我的util.java(非活动)中的方法(如
)在对话框中指定此布局private static Dialog progressDialog = null;
public static void showLoadingProgress(String msg){
Log.d("EditorUtil", "show loading progress"+progressDialog);//No i18n
progressDialog = new Dialog(MainActivity.getActivity());
progressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
progressDialog.setCancelable(false);
progressDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
progressDialog.setContentView(R.layout.customprogress);
TextView message = (TextView)progressDialog.findViewById(R.id.progressmsg);
message.setText(msg);
progressDialog.show();
Log.d("",progressDialog.isShowing()+""); //No i18n
}
调用此方法时,无法看到对话框,但日志正确打印。请帮忙解决这个问题。
答案 0 :(得分:0)
创建自定义对话框
如果您想要对话框的自定义设计,您可以使用布局和窗口小部件元素为对话框窗口创建自己的布局。定义布局后,将根View对象或布局资源ID传递给setContentView(View)。
For example, to create the dialog shown to the right:
Create an XML layout saved as custom_dialog.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在LinearLayout中定义了ImageView和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);
答案 1 :(得分:0)
View dialogRoot = getLayoutInflater().inflate(R.layout.sync_bars, null);
cancelDialog.setView(dialogRoot);
ProgressBar hbar = (ProgressBar) dialogRoot.findViewById(R.id.progressBar);
您必须引用您的进度条与其进行互动。
答案 2 :(得分:0)
我不确定你背后的原因可能是什么,但我可以提出一些你可能会觉得有用的事情
使用上下文而不是getActivity。
progressDialog = new Dialog(MainActivity.getActivity());
应该替换为必须添加到util类的上下文,并且应该通过构造函数
传递 progressDialog = new Dialog(context);
我希望你没有通过getActivity方法传递Activity的对象。
你可以将所有这些东西添加到R.style.Your主题
3.如果为对话框创建一个单独的类,像这样扩展Dialog类
public class TextEditDialog extends Dialog implements OnClickListener {
Button cancelButton;
public Button okBtn;
TextView title;
EditText nameField,password;
public static long id;
public static int code;
CheckBox rememberme;
Context c;
TableRow bottomoftextdialog;
TextView bottomtexttochange,toptexttochange;
public TextEditDialog(Context context) {
super(context,R.style.Theme_Custom);
c=context;
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
nameField = (EditText) findViewById(R.id.Uname);
password= (EditText) findViewById(R.id.password);
rememberme=(CheckBox)findViewById(R.id.rememberme);
okBtn = (Button)findViewById(R.id.login);
bottomoftextdialog=(TableRow)findViewById(R.id.bottomoftextdialog);
bottomtexttochange=(TextView)findViewById(R.id.bottomtexttochange);
toptexttochange=(TextView)findViewById(R.id.toptexttochange);
okBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dismiss();
}
});
}
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
this way you will be able to manage things really well.