好的,所以我一直在努力使用 android 对话框,这就是我想要做的。我有一个要显示的对话框的自定义 xml 布局,我需要在事件触发时更改该布局。这里的问题是,每当我想更改对话框布局中的任何视图时,窗口每次都会结束,我似乎无法维护它的参数......
这是我创建对话框的方式:
final Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_game_details);
dialog.setCancelable(false);
dialog.show();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.getWindow().setGravity(Gravity.TOP);
我如何访问主要布局元素以添加/删除视图:
final LinearLayout ll_dialog_root_view = (LinearLayout) dialog.findViewById(R.id.ll_dialog_game_root_view);
final LinearLayout ll_dialog_container_view = (LinearLayout) dialog.findViewById(R.id.ll_dialog_game_body_container);
以及 xml 布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_dialog_game_root_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/transparent"
android:paddingLeft="11dp"
android:paddingRight="11dp"
android:paddingTop="32.5dp"
android:paddingBottom="20dp"
android:orientation="vertical" >
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:background="@drawable/border_bottom_1_5dp_grey"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/ll_dialog_game_title_bar"
android:layout_width="match_parent"
android:layout_height="40dp"
android:paddingLeft="11dp"
android:paddingRight="11dp"
android:layout_marginBottom="15dp"
android:layout_marginTop="@dimen/dialog_cont_title_margin_top"
android:background="@color/gray">
<ImageView
android:id="@+id/iv_dialog_game_close"
android:layout_width="12dp"
android:layout_height="22dp"
android:layout_marginRight="7dp"
android:layout_gravity="center_vertical"
android:src="@drawable/ico_arrow_back" />
<TextView
android:id="@+id/tv_dialog_game_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:textColor="@color/white"
android:text="title"
android:textSize="21dp"
android:ellipsize="end"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ll_dialog_game_body_container"
android:orientation="vertical">
<ScrollView
android:id="@+id/sv_dialog_game_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="21.5dp"
android:layout_marginRight="21.5dp"
android:layout_marginBottom="14.5dp"
android:requiresFadingEdge="vertical"
android:fillViewport="true"
>
<TextView
android:id="@+id/tv_dialog_game_description"
android:text="..."
android:textSize="14dp"
android:textColor="@color/black"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</ScrollView>
<TextView
android:id="@+id/tv_dialog_game_info"
android:layout_marginLeft="21.5dp"
android:layout_marginRight="21.5dp"
android:text="param: value"
android:textSize="13dp"
android:lineSpacingExtra="2dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_marginRight="@dimen/dialog_cont_title_margin_top"
android:layout_marginLeft="@dimen/dialog_cont_title_margin_top"
android:layout_marginTop="32dp"
android:layout_height="40dp"
android:layout_marginBottom="@dimen/dialog_cont_title_margin_top"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/iv_dialog_game_share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:padding="6dp"
android:background="@color/bt_share_default"
android:src="@drawable/ico_white_social_share"/>
<LinearLayout
android:id="@+id/ll_dialog_game_start"
android:layout_alignParentRight="true"
android:layout_width="170dp"
android:layout_height="40dp"
android:paddingRight="14dp"
android:background="@color/bt_catalog_show_more_categories"
>
<ImageView
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="center_vertical"
android:src="@drawable/ico_white_av_play"
android:layout_marginRight="11.5dp"
android:layout_marginLeft="11.5dp"
/>
<!-- android:src="@drawable/ico_white_av_play" -->
<TextView
android:layout_width="wrap_content"
android:gravity="center_vertical"
android:textColor="@color/white"
android:layout_height="match_parent"
android:textSize="20dp"
android:text="¡Jugar!"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
<ProgressBar
android:id="@+id/pb_dialog_game_loading"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal" />
<TextView
android:id="@+id/tv_dialog_game_loading"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:text="..."
android:textColor="@color/black"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/tv_dialog_game_countdown"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:text="Countdown TextView"
android:textColor="@color/black"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</LinearLayout>