对话框外部视图

13
我希望有像这样的效果:

playstorereview

用户的个人资料图片在对话框的边界上“突出”。
我尝试了一切可能的组合方式来调整裁剪,动态创建视图并将其添加到根内容视图中,在Dialog.setCustomTitle()中使用单独的视图加载进去,还尝试了在Images onDraw()方法中进行修改以及应用各种边界/位置修改的技巧——但无论如何都会裁剪和分割图片。
我甚至已经反编译了Play Store APK并查看他们是如何实现的。不幸的是,资源文件没有提供太多信息,而且我在Smali中也找不到任何东西。
谁能帮帮我? 求求大家... :-(
编辑:我只是特别指的是对话框顶部的用户个人资料图片,而不是对话框本身。

你尝试过使用带有“dialog-theme”的Activity吗?(https://dev59.com/jnI-5IYBdhLWcg3wMFMf) - i_turo
4个回答

34

对话框方法:

Dialog dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.mhp);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    dialog.show();  

mhp.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/transparent" >

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="57dp"
    android:background="#f00"
    android:orientation="vertical" >
</LinearLayout>

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:src="@drawable/ic_launcher" />

</RelativeLayout>  

结果
结果图片


太棒了,谢谢!我想使用AlertDialog但透明度无法奏效。对我来说,这个做法很管用: https://dev59.com/7nPYa4cB1Zd3GeqPj4Rk#20015598 - eth0

4
看起来他们只是在 ImageView 中使用了一个圆形图像,并且有一个布局与图片大小一半的顶部边距。
因此,如果我的图片尺寸是 100px,我应该使用 50dip 来在屏幕中心显示图像。
 dialog.xml

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="200dip"
    android:layout_marginTop="50dip" 
    android:background="@android:color/holo_green_dark" >
</LinearLayout>

<ImageView
    android:id="@+id/imageview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:contentDescription="@string/app_name"
    android:src="@drawable/download" />

然后您只需使用透明背景的对话框

Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog);
dialog.setCancelable(false);
dialog.getWindow().setBackgroundDrawable(
                                 new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.show();

输出

展示图


4

为了改进@MHP的答案,你还可以添加cardView来使对话框的角变圆。

就像这样:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/transparent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="57dp"
    android:background="@android:color/transparent"
    android:orientation="vertical">

    <android.support.v7.widget.CardView xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/llMainContents"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FAFAFA"
        android:orientation="vertical"
        app:cardCornerRadius="2dp" />
</LinearLayout>

<ImageView
    android:id="@+id/ivIcon"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:padding="8dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:contentDescription="@null"
    android:src="@mipmap/ic_launcher" />

</RelativeLayout>

结果将是: 这里输入图片描述

惊人的答案。在所有解决方案中,我测试过了,但是你的答案是完美的。而且运行良好。你救了我的一天。 - Daxesh Vekariya

3

首先,您应该使对话框的背景透明。

对于 DialogFragment,请参考:https://stackoverflow.com/a/23729785

对于 AlertDialog,请参考:https://dev59.com/Mm025IYBdhLWcg3wW0oc#19175448

其次,以以下方式组合您的布局:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_marginTop="30dp"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@android:color/darker_gray">

        <TextView
            android:layout_centerHorizontal="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:text="blablabla"/>
    </RelativeLayout>

    <ImageView
        android:id="@+id/iv1"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ic_launcher"/>

</RelativeLayout>

其中 iv1 是页眉图片。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接