【问题标题】:Android Custom AlertDialog not drawn correctlyAndroid 自定义 AlertDialog 未正确绘制
【发布时间】:2014-07-25 04:39:27
【问题描述】:

我正在尝试自定义 AlertDialog 的外观。问题是我不知道如何摆脱出现在底部的第一个 TextView 和 EditText 上方(灰色按钮区域上方)的黑线。

这是我的 XML。我错过了什么?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="@color/butGreyBack"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

      <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:layout_weight="30"
        android:text=""
        android:background="#00000000"
        android:textColor="@android:color/white"
        android:textSize="22sp"        
        android:layout_margin="0dp"     
        android:gravity="center" />

      <TextView
        android:id="@+id/tv_prompt"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:layout_weight="30"
        android:text=""
        android:background="#00000000"
        android:textColor="@android:color/white"
        android:textSize="18sp"        
        android:layout_margin="0dp"     
        android:gravity="center" />

    <EditText
        android:id="@+id/edit_text"
        android:inputType="textCapSentences|textAutoCorrect"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginBottom="4dp"/>
</LinearLayout>

这也是代码 - 这可能是问题所在:

  View alertView;

  AlertDialog.Builder builder = new AlertDialog.Builder( mContext );

  LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
  alertView = inflater.inflate(R.layout.alert_dialog_text, null);

  builder.setView( alertView );

  AlertDialog alertDialog = builder.create();
  alertDialog.setView( alertView, 0,0,0,0 );

  final EditText input = (EditText) alertView.findViewById( R.id.edit_text );
  TextView tvTitle  = (TextView) alertView.findViewById( R.id.tv_title );
  TextView tvPrompt = (TextView) alertView.findViewById( R.id.tv_prompt );

  tvTitle.setText( "Rename Playlist" );
  tvPrompt.setText( "Enter new Playlist name." );

  // Set up the buttons
  builder.setPositiveButton( "OK", new DialogInterface.OnClickListener()
  {
    @Override
    public void onClick( DialogInterface dialog, int which )
    {
      mNewName = input.getText().toString();
    }
  } );

  builder.setNegativeButton( "Cancel", new DialogInterface.OnClickListener()
  {
    @Override
    public void onClick( DialogInterface dialog, int which )
    {
      mNewName = "";
      dialog.cancel();
    }
  } );

  builder.show();

【问题讨论】:

  • 使父线性布局的宽度和高度与父匹配

标签: android xml customization android-alertdialog android-custom-view


【解决方案1】:

@sparky...

1) 在 Values 文件夹中的 xml 并添加此代码

包装内容 包装内容 @颜色/透明1 错误的 真的 并将此主题引用到您的对话框中,如下所示

对话框 dialog = new Dialog(activity, R.style.CustomDialogTheme); 然后将您的自定义对话框布局 Xml 文件设置为 setContentView。

dialog.setContentView(R.layout.customdialog);

2)你没有在最后关闭父 LinearLayout

并且您正在为父布局使用#000000 颜色代码,这将在背景中显示黑色

尝试更改底部颜色的颜色代码将有机会。并使用

requestWindowFeature(Window.FEATURE_NO_TITLE);

在您的对话框类中,这将删除对话框标题,因此您的顶部颜色也将删除..

如果您需要任何帮助,请告诉我

谢谢

【讨论】:

  • 谢谢 - 会试一试 - 我也刚刚发布了我的代码,以防出现问题。
  • 好的。试试我给你的建议。如果你仍然遇到问题。我会向你发送没有黑线和边框的代码。你也接受了我的回答。
  • 如果您的回答对我来说是正确的,我当然会接受。但是,我不明白你的建议。我最初剪切并粘贴了格式不正确的 XML,所以看起来 XML 是错误的 - 现在可以了。我不明白您在 1) 和 2) 中的建议 - 我没有任何 #000000 值。我有#00000000(透明)。
  • requestWindowFeature(Window.FEATURE_NO_TITLE);在您的对话框类中,这将删除对话框标题,因此您的顶部颜色也将删除..
  • 我试过了:alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);在 setView() 之后并没有帮助:(
【解决方案2】:

我最终通过实现我自己的自定义对话框而不是使用 AlertDialog 解决了这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    相关资源
    最近更新 更多