【问题标题】:How to remove title from custom dialog?如何从自定义对话框中删除标题?
【发布时间】:2015-12-02 11:08:06
【问题描述】:

我实现了一个布局,以便成为我的自定义对话框布局,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:background="@color/gray_back"
   xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
    android:id="@+id/mainLayout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dp"
    android:layout_centerInParent="true">

    <ImageView
        android:src="@drawable/user"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="center"
        android:layout_gravity="center"
        android:contentDescription="@string/app_name" />

    <EditText
        android:id="@+id/username"
        android:background="@android:color/white"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="20dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:gravity="center"
        android:layout_gravity="center"
        android:hint="@string/new_profile_name" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:background="?android:attr/dividerHorizontal" />

</LinearLayout>

<LinearLayout
    style="?android:attr/buttonBarStyle"
    android:layout_below="@id/mainLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingTop="0dip"
    android:measureWithLargestChild="true">

    <Button
        android:id="@+id/cancel"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Cancel"/>
    <Button
        android:id="@+id/ok"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Ok"/>
</LinearLayout>

我这样以编程方式创建它:

final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog_add_profile);

Button dialogButton = (Button) dialog.findViewById(R.id.cancel);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
         dialog.dismiss();
       }
   });
 dialog.show();

然后我的对话框显示如下:

如何删除图像到布局顶部的剩余空间?

我想试试这个:

dialog.setWindowFeature(Window.WINDOW_NO_TITLE);

但它崩溃了......我怎样才能删除剩余的空间?

【问题讨论】:

  • 粘贴崩溃日志。

标签: android dialog android-dialog


【解决方案1】:

新建一个样式,添加:

<style name="NoTitleDialog" parent="@android:Theme.Dialog">
<item name="android:windowNoTitle">true</item> </style>

最后:

Dialog d = new Dialog(this, R.style.nameOfStyle);

编辑: This answer

【讨论】:

  • 这是最干净的解决方案!
【解决方案2】:

尝试使用 DialogFragment 类,它不会在自定义视图上强制标题。您可以在您的活动中将其设为静态嵌套类。

public static class MyDialogFragment extends DialogFragment {
  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setView(R.layout.custom_view);
    return builder.create();
  }
}

让您的 Activity 扩展 AppCompatActivity,使用导入

import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;

然后显示对话框实例化对话框片段调用 show() 传递支持片段管理器和字符串标记,如下所示:

MyDialogFragment dialogFragment = new MyDialogFragment();
    dialogFragment.show(getSupportFragmentManager(), "f1");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-29
    • 1970-01-01
    • 2011-08-23
    • 2018-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多