我想在中心垂直位置显示自定义对话框,但它显示在顶部。上面的第一个是我的xml文件,第二个是在真实设备中运行后输出。这是对话框的代码。
//alert dialog code
//add theme to display on whole page
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this,
android.R.style.Theme_Holo_Light_NoActionBar_TranslucentDecor);
LayoutInflater inflater = this.getLayoutInflater();
//custom layout
View dialogView = inflater.inflate(R.layout.custom_bar, null);
dialogBuilder.setView(dialogView);
//button to close dialog box
Button closeButton = (Button)
dialogView.findViewById(R.id.closeButton);
final AlertDialog alertDialog = dialogBuilder.create();
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
alertDialog.dismiss();
}
});
//show dialog
alertDialog.show();
答案 0 :(得分:0)
将此代码用于屏幕中心的“警报”对话框。
在您的课程中添加此内容,并根据需要更改代码。
//Internet Alert Dialog
public static void InternetAlert(final Context _A) {
final Dialog dialog = new Dialog(_A, R.style.CustomDialogStyle);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCanceledOnTouchOutside(true);
dialog.setContentView(R.layout.dialog_internet_connection);
final Button Yes = (Button) dialog.findViewById(R.id.btn_yes);
Yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
如果要填充整个屏幕,只需将FrameLayout高度更改为match_parent
即可<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="320dp"
android:layout_height="wrap_content"
android:layout_gravity="center">
<android.support.v7.widget.CardView
android:id="@+id/cv_SignOut"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="?attr/actionBarSize"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_marginTop="?attr/actionBarSize"
app:cardBackgroundColor="#0D000000"
app:cardCornerRadius="6dp"
app:cardElevation="3dp"
app:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent">
<TextView
android:id="@+id/title_SignOut"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="@color/textViewColorPrimary"
android:gravity="center"
android:minHeight="?attr/actionBarSize"
android:text="@string/info"
android:textAppearance="?attr/textAppearanceSearchResultTitle"
android:textColor="@color/colorWhite" />
<TextView
android:id="@+id/signOutAlert"
style="@android:style/TextAppearance.DeviceDefault.SearchResult.Subtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/title_SignOut"
android:background="@color/colorBackground"
android:gravity="center"
android:minHeight="120dip"
android:paddingBottom="@dimen/activity_horizontal_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_horizontal_margin"
android:text="@string/info_internet_check"
android:textColor="@color/colorPrimary" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/signOutAlert"
android:background="@color/white"
android:orientation="horizontal"
android:weightSum="1">
<Button
android:id="@+id/btn_yes"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_marginRight="2dip"
android:layout_weight="1"
android:background="@color/colorBackground"
android:gravity="center"
android:minHeight="?attr/actionBarSize"
android:text="OK"
android:textAppearance="?attr/textAppearanceSearchResultTitle"
android:textColor="@color/colorAccent" />
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</FrameLayout>
希望这可能会有所帮助:)
答案 1 :(得分:0)
在custom_bar
put android:gravity="center"
答案 2 :(得分:0)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="abc"
android:textSize="30sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="abc"
android:textSize="30sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="abc"
android:textSize="30sp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Done" />
</LinearLayout>
</LinearLayout>
答案 3 :(得分:0)
------
dialogBuilder.showAsDropDown(dialogView, 50, 400, Gravity.CENTER);
alertDialog.show();
----
将该行添加到您的代码中。它会将弹出窗口设置为居中。
答案 4 :(得分:0)
您应该这样写。
AlertDialog dialog = new AlertDialog.Builder(this, R.style.Dark).create();
if (dialog.getWindow() != null)
{
Window window = dialog.getWindow();
window.setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.background_dark);
}
这将在屏幕中央显示警报对话框。