如何将imageview放在自定义对话框的中心顶部?

时间:2017-01-10 07:12:05

标签: android android-layout alertdialog

我正在创建一个自定义对话框,我希望将ImageView放在对话框的中心顶部,以便完成此操作。

enter image description here

4 个答案:

答案 0 :(得分:1)

您可以根据自己的想法将Views放在LinearLayout内。

对话方法:

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>  

结果(避免BG)

enter image description here

答案 1 :(得分:1)

试试这个

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>




<LinearLayout
   android:layout_width="match_parent"
   android:layout_height="300dp"
    android:layout_marginTop="90dp"

     android:background="@color/colorPrimary"
   >
  </LinearLayout>

 <ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_marginTop="20dp"
    android:layout_gravity="center_horizontal"
    android:src="@mipmap/ic_launcher"
    />
</FrameLayout>

答案 2 :(得分:1)

enter image description here

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="@dimen/_15dp"
        android:background="@android:color/transparent"
        android:padding="@dimen/_10dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:orientation="vertical">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="110dp"
                android:background="@android:color/transparent">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <View
                        android:layout_width="wrap_content"
                        android:layout_height="0dp"
                        android:layout_weight=".47"
                        android:background="@drawable/white_transparent_k" />

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight=".53"
                        android:background="@drawable/radius_top" />
                </LinearLayout>

                <ImageView
                    android:layout_width="110dp"
                    android:layout_height="110dp"
                    android:layout_centerInParent="true"
                    android:layout_marginTop="-50dp"
                    android:src="@drawable/update_avilable" />
            </RelativeLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="@drawable/radius_bottom"
                android:orientation="vertical"
                android:paddingLeft="8dp"
                android:paddingRight="8dp"
                android:paddingBottom="8dp">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:padding="@dimen/_4dp"
                    android:text="@string/update_app_titile"
                    android:textAlignment="center"
                    android:textColor="@color/black" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:padding="@dimen/_4dp"
                    android:text="@string/update_app_text"
                    android:textAlignment="center" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="@dimen/_10dp"
                    android:gravity="center"
                    android:orientation="horizontal"
                    android:weightSum="2">
                    <!--#BDBDBD-->
                    <TextView
                        android:id="@+id/btn_cancel"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginRight="@dimen/_5dp"
                        android:background="@drawable/round_shape_fade_bg"
                        android:paddingBottom="@dimen/_5dp"
                        android:paddingLeft="30dp"
                        android:paddingRight="30dp"
                        android:paddingTop="@dimen/_5dp"
                        android:text="@string/update_cancel"
                        android:textColor="@color/white" />

                    <TextView
                        android:id="@+id/btn_confirm"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginBottom="@dimen/_10dp"
                        android:layout_marginLeft="@dimen/_5dp"
                        android:layout_marginTop="@dimen/_10dp"
                        android:background="@drawable/round_shape_blue_bg"
                        android:paddingBottom="@dimen/_5dp"
                        android:paddingLeft="20dp"
                        android:paddingRight="20dp"
                        android:paddingTop="@dimen/_5dp"
                        android:text="@string/update_confirm"
                        android:textColor="@color/white" />
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>

    </RelativeLayout>


            public void isAppUpdateAvailable() {
            LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
            View view = layoutInflater.inflate(R.layout.update_available_dialog, null);
            final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

            builder.setView(view);

            final AlertDialog alertDialog = builder.create();
            alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

            TextView btnConfirm,btn_cancel;
            btn_cancel=view.findViewById(R.id.btn_cancel);
            btnConfirm=view.findViewById(R.id.btn_confirm);

            btn_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    alertDialog.dismiss();
                }
            });

            btnConfirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {              
                    alertDialog.dismiss();
                }
            });

            alertDialog.show();
        }


    **white_transparent_k.xml**

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <solid
            android:color="@android:color/transparent"/>
        <corners
            android:radius="15dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />
    </shape>

    **radius_top.xml**

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <solid
            android:color="@color/white"/>
        <corners
            android:topLeftRadius="15dp"
            android:topRightRadius="15dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />
    </shape>

    **radius_bottom.xml**

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <solid
            android:color="@color/white"/>
        <corners
            android:bottomLeftRadius="15dp"
            android:bottomRightRadius="15dp" />
        <padding
            android:left="10dp"
            android:right="10dp"
            />
    </shape>

答案 3 :(得分:0)

在相对布局框中设置此imageview,并为imageview标记设置这两个属性

 android:layout_centerHorizontal="true"
   android:layout_marginTop="-50dp"