【问题标题】:View outside a Dialogs bounds在对话框边界之外查看
【发布时间】:2014-10-11 05:34:06
【问题描述】:

我想要这样的东西:

用户个人资料图片在对话框边界“弹出”。

我已经尝试了所有方法:在阳光下使用每个可能的组合进行剪辑,在对话框之后动态创建视图并将其添加到根内容视图,使用单独的视图并使用 Dialog.setCustomTitle() 加载它,破解 Images onDraw() 方法并应用各种边界/位置破解 --- 但无论如何,图像总是被剪裁并分成两半。

我什至已经到了反编译 Play Store APK 的程度,看看他们是如何做到的。不幸的是,资源文件并没有提供太多,我在 Smali 中找不到任何东西。

有人可以帮忙吗?请... :-(

编辑:我只是专门讨论对话框顶部的用户个人资料图片,而不是对话框本身。

【问题讨论】:

标签: android android-layout android-ui android-dialog


【解决方案1】:

对话方式:

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>  

结果

【讨论】:

【解决方案2】:

看起来他们只是在 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();

输出

【讨论】:

    【解决方案3】:

    只是为了改进@MHP 的answer,您还可以添加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>
    

    结果将是:

    【讨论】:

    • 惊人的答案。首先是我测试的所有解决方案,但您的答案是完美的。并且工作正常。你拯救了我的一天。
    【解决方案4】:

    首先,您应该使对话框的背景透明。 对于 DialogFragment:https://stackoverflow.com/a/23729785。 对于 AlertDialog:https://stackoverflow.com/a/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是标题图片

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-30
      • 2011-11-28
      • 2018-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多