弹出窗口打开时,Android自定义布局中断

时间:2016-02-04 16:06:55

标签: android android-layout android-relativelayout

所以我制作了一个自定义布局,当用户点击图标时,我想在弹出窗口中使用它。这就是我的布局:

enter image description here

这是我的xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/rectangle"
    android:id="@+id/popupMainLayout">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_margin="20dp"
        android:background="@drawable/icon_drawing_preview_rectangle"
        android:id="@+id/relativeLayout">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="false"
            android:layout_alignParentEnd="true"
            android:layout_centerHorizontal="true"
            android:gravity="top"
            android:id="@+id/previewIcon">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Large Text"
                android:id="@+id/infoTitle"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="5dp" />

            <com.example.app.TouchImageView
                android:layout_width="100dp"
                android:layout_height="70dp"
                android:id="@+id/infoPic"
                android:layout_centerHorizontal="true"
                android:layout_gravity="center_horizontal|bottom"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="5dp" />

        </LinearLayout>


    </RelativeLayout>

    <ImageView
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:id="@+id/ivCopy"
        android:background="@drawable/copy"
        android:layout_below="@+id/relativeLayout"
        android:layout_alignLeft="@+id/relativeLayout" />

    <ImageView
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:id="@+id/ivEdit"
        android:background="@drawable/edit"
        android:layout_alignTop="@+id/ivCopy"
        android:layout_centerHorizontal="true" />

    <ImageView
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:id="@+id/ivDelete"
        android:background="@drawable/delete"
        android:layout_alignTop="@+id/ivEdit"
        android:layout_alignRight="@+id/relativeLayout"
        android:layout_alignEnd="@+id/relativeLayout" />

    <ImageView
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:id="@+id/ivPaste"
        android:background="@drawable/paste"
        android:layout_centerVertical="true"
        android:layout_alignLeft="@+id/ivCopy"
        android:layout_alignStart="@+id/ivCopy" />

    <ImageView
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:id="@+id/ivRotate"
        android:layout_alignTop="@+id/ivPaste"
        android:layout_centerHorizontal="true"
        android:background="@drawable/rotate" />

    <ImageView
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:id="@+id/ivCut"
        android:background="@drawable/cut"
        android:layout_centerVertical="true"
        android:layout_alignRight="@+id/relativeLayout"
        android:layout_alignEnd="@+id/relativeLayout" />

    <Button
        android:layout_width="120dp"
        android:layout_height="40dp"
        android:text="Cancel"
        android:id="@+id/dismiss"
        android:background="@drawable/custom_buttons"
        android:textSize="15dp"
        android:gravity="center"
        android:layout_gravity="center_horizontal"
        android:layout_below="@+id/ivRotate"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="15dp" />

</RelativeLayout>

然而,当我点击我的图标时,我的布局会被破坏,图标不会排成一行。这就是它最终看起来像: enter image description here

它有什么特别的原因吗?

编辑:我如何启动弹出窗口:

 //inflate our popup layout, set the title and set the icon
                    LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
                    View popupView = layoutInflater.inflate(R.layout.popup_info_layout, null);

                    setOperationsClicklisteners(popupView);//only setting imageview click listeners here

                    TextView title = (TextView)popupView.findViewById(R.id.infoTitle);

                    title.setText(ic.getName());
                    ImageView popupIcon = (ImageView)popupView.findViewById(R.id.infoPic);

                    popupWindow = new PopupWindow(
                            popupView,
                            ViewGroup.LayoutParams.WRAP_CONTENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT);

                    Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
                    btnDismiss.setOnClickListener(new Button.OnClickListener()
                    {
                        @Override
                        public void onClick(View v)
                        {
                            popupWindow.dismiss();
                        }
                    });

                    popupWindow.setClippingEnabled(true);
                    popupWindow.showAtLocation (v, Gravity.CENTER_VERTICAL, 0, 0);

2 个答案:

答案 0 :(得分:1)

  1. 您的布局似乎有误。
  2. 您应该将src用于ImageView图标,而不是background

    <RelativeLayout
        android:layout_below="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView
            src="@drawable/copy"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"/>
        <ImageView
            src="@drawable/edit"
            android:layout_alignParentTop="true"
            android:layout_centerhorizontal ="true"/>
        <ImageView
            src="@drawable/delete"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"/>
        <ImageView
            src="@drawable/paste"
            android:layout_marginTop="20dp"
            android:layout_alignParentLeft="true"/>
        <ImageView
            src="@drawable/rotate"
            android:layout_alignTop="@+id/paste"
            android:layout_centerHorizontal="true"/>
        <ImageView
            src="@drawable/cut"
            android:layout_alignTop="@+id/paste"
            android:layout_alignParentRight="true"/>
        <Button
            android:layout_below="@+id/rotate"
            android:layout_centerHorizontal="true"/>
    </RelativeLayout>
    

答案 1 :(得分:0)

在阅读了评论和建议的答案后,我将它们合并在一起并使其正常工作。 @Vivek Mishra建议的主要问题是我的布局宽度/高度设置为fill_parent,而在我的代码中,我使用wrap_content对其进行了充气。

我的新风格有效:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="360dp"
    android:layout_height="340dp"
    android:background="@drawable/rectangle"
    android:id="@+id/popupMainLayout">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_margin="20dp"
        android:background="@drawable/icon_drawing_preview_rectangle"
        android:id="@+id/relativeLayout">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="false"
            android:layout_alignParentEnd="true"
            android:layout_centerHorizontal="true"
            android:gravity="top"
            android:id="@+id/previewIcon">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Large Text"
                android:id="@+id/infoTitle"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="5dp" />

            <com.example.app.TouchImageView
                android:layout_width="100dp"
                android:layout_height="70dp"
                android:id="@+id/infoPic"
                android:layout_centerHorizontal="true"
                android:layout_gravity="center_horizontal|bottom"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="5dp" />

        </LinearLayout>


    </RelativeLayout>

    <ImageView
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:id="@+id/ivCopy"
        android:layout_below="@+id/relativeLayout"
        android:layout_alignLeft="@+id/relativeLayout"
        android:layout_alignStart="@+id/relativeLayout"
        android:src="@drawable/copy" />

    <ImageView
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:id="@+id/ivEdit"
        android:layout_alignTop="@+id/ivDelete"
        android:layout_centerHorizontal="true"
        android:src="@drawable/edit" />

    <ImageView
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:id="@+id/ivDelete"
        android:layout_below="@+id/relativeLayout"
        android:layout_alignRight="@+id/relativeLayout"
        android:layout_alignEnd="@+id/relativeLayout"
        android:src="@drawable/delete" />

    <ImageView
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:id="@+id/ivPaste"
        android:layout_below="@+id/ivCopy"
        android:layout_alignLeft="@+id/ivCopy"
        android:layout_alignStart="@+id/ivCopy"
        android:layout_marginTop="25dp"
        android:src="@drawable/paste" />

    <ImageView
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:id="@+id/ivRotate"
        android:layout_alignTop="@+id/ivPaste"
        android:layout_alignLeft="@+id/ivEdit"
        android:layout_alignStart="@+id/ivEdit"
        android:src="@drawable/rotate" />

    <ImageView
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:id="@+id/ivCut"
        android:layout_alignTop="@+id/ivRotate"
        android:layout_alignLeft="@+id/ivDelete"
        android:layout_alignStart="@+id/ivDelete"
        android:src="@drawable/cut" />

    <Button
        android:layout_width="90dp"
        android:layout_height="50dp"
        android:text="cancel"
        android:id="@+id/dismiss"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"
        android:background="@drawable/custom_buttons" />

</RelativeLayout>