简体   繁体   中英

How to put imageview on center top of custom dialog?

I am creating a custom dialog where I want to put ImageView on center top of dialog box how this can be done.

在此处输入图片说明

You can put your Views , inside LinearLayout as per your idea.

Dialog method:

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>  

Result(avoid BG)

在此处输入图片说明

Try this

<?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>

在此处输入图片说明

    <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>

set this imageview in relative layout box and set these two property for imageview tag

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM