自定义对话框,图像在视图边界外?

时间:2015-10-04 11:51:44

标签: android

我尝试创建一个Custom Dialog this,我跟随此answer,但将layout_gravity更改为此android:layout_gravity="center_horizontal|top"它根据我的要求放在Dialog的中心,但问题是,如果我想让ImageView更大,它会破坏xml,也会Dialog它本身太小了,因为我只有TextViewEditText所以我做错了什么?

这是我的xml。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center" >

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="15dp" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="#fff"
            android:orientation="vertical"
            android:padding="10dp" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Choose the receiver"
                android:textColor="@color/blue"
                android:textSize="16dp" />

            <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:background="@drawable/edittext_style"
                android:hint="Username"
                android:padding="5dp"
                android:singleLine="true"
                android:textColorHint="@color/wallet_holo_blue_light" />

        </LinearLayout>
    </FrameLayout>

    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_gravity="center_horizontal|top"
        android:src="@drawable/icon_mail" />
</FrameLayout>
</FrameLayout>

输出

enter image description here

我还要DrawableDialog放在LinearLayout上以创建一个颜色变差的边框,如果我将其放在<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item> <shape android:shape="rectangle" > <solid android:color="@color/white" /> <corners android:radius="12dip" /> <stroke android:width="1dip" android:color="@color/wallet_holo_blue_light" /> </shape> </item> 上,它就不会显示起来。

这是rounded_border.xml

ImageView

如果我更改{{1}}的大小,请执行以下操作:

enter image description here

2 个答案:

答案 0 :(得分:1)

你可以试试这个:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="30dp">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
                <View
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="0.5" />
                <View
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="0.5"
                    android:background="#fff" />
            </LinearLayout>
            <ImageView
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_centerInParent="true"
                android:src="@drawable/icon_mail" />
        </RelativeLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="#fff"
            android:orientation="vertical"
            android:padding="8dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Choose the receiver"
                android:textColor="@color/blue"
                android:textSize="16dp" />
            <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:background="@drawable/edittext_style"
                android:hint="Username"
                android:padding="5dp"
                android:singleLine="true"
                android:textColorHint="@color/wallet_holo_blue_light" />
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

重要的是保持ImageView宽度/高度(示例中为30dp)与父RelativeLayout高度对齐。

答案 1 :(得分:0)

你需要在你的根框架布局中添加一些填充:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:padding = "40dp">

还在图片视图中为top添加了一些负余量

<ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_gravity="center_horizontal|top"
        android:src="@drawable/icon_mail" 
        android:layout_marginTop="-20dp"/>

根据需要调整值。

另外你使用了一个不必要的framelayout,一个在你的根布局中,你必须得到关于它的lint警告。

相关问题