我创建了一个警告对话框并使用setView
布局高度设置为<ImageView
android:id="@+id/iv_close"
android:layout_marginTop="10dp"
android:layout_marginRight="2dp"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_gravity="right"
android:src="@drawable/close"/>
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="20sp"
android:textColor="@color/text_color_dark"
android:text="Heading/Title i.e. Lorem Ipsum"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="@color/text_color_dark"
android:text="Lorem Ipsum is simply dummysoftware like Aldus PageMaker including versions of Lorem Ipsum dummysoftware like Aldus PageMaker including versions of Lorem Ipsum dummysoftware like Aldus PageMaker including versions of Lorem Ipsum dummysoftware like Aldus PageMaker including versions of Lorem Ipsum dummysoftware like Aldus PageMaker including versions of Lorem Ipsum dummysoftware like Aldus PageMaker including versions of Lorem Ipsum dummysoftware like Aldus PageMaker including versions of Lorem Ipsum dummysoftware like Aldus PageMaker including versions of Lorem Ipsum dummysoftware like Aldus PageMaker including versions of Lorem Ipsum ."/>
<uk.org.humanfocus.hfi.CustomViews.ButtonColorDark
android:id="@+id/btn_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="100dp"
android:minHeight="30dp"
android:textSize="18sp"
android:layout_marginTop="5dp"
android:layout_gravity="center"
android:text="Ok"/>
</LinearLayout>
</ScrollView>
当对话框中的文字足以填满屏幕时,对话框将裁剪顶视图。
对话框布局的XML:
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.AppDialogTheme));
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
View dialog = inflater.inflate(R.layout.dialog_display_information, null);
dialogBuilder.setView(dialog);
dialogBuilder.setCancelable(false);
final AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
((TextView)dialog.findViewById(R.id.tv_title)).setText(title);
((TextView)dialog.findViewById(R.id.tv_text)).setText(text);
dialog.findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.cancel();
}
});
dialog.findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.cancel();
}
});
return alertDialog;
对话的Java代码:
<meta-data
android:name="AA_MODELS"
android:value="com.myapp.model.Item, com.myapp.model.Category" />