Android: Layer drawable loses information

余生长醉 提交于 2019-12-25 03:42:38

问题


I am messing around with creating my own dialog class, but I've run into an interesting issue. I use a layer-list drawable for the dialog's close button, but for some reason, the shadow layer disappears after the first time I show the dialog. You can see in the image below:

I really have absolutely no idea what could be causing this. I've tried debugging, but I wasn't able to find any leads. Does anyone have any ideas? My code is below.

TwoColorDialog.java

public class TwoColorDialog extends DialogFragment {
    private static final String TAG = "TwoColorDialog";

    private ViewGroup _root;

    private TextView _textTitle;
    private ImageView _buttonClose;
    private Button _button1;
    private Button _button2;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Dialog dialog = new Dialog(getActivity());
        _root = (ViewGroup) getActivity().getLayoutInflater().inflate(R.layout.two_color_dialog, null);

        _textTitle   =  (TextView) _root.findViewById(R.id.dialog_title);
        _buttonClose = (ImageView) _root.findViewById(R.id.dialog_close_button);
        _button1     =    (Button) _root.findViewById(R.id.dialog_button1);
        _button2     =    (Button) _root.findViewById(R.id.dialog_button2);

        dialog.requestWindowFeature(STYLE_NO_TITLE);
        View decorView = dialog.getWindow().getDecorView();
        decorView.setBackgroundResource(getResources().getColor(android.R.color.transparent));

        dialog.setContentView(_root);

        _buttonClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.cancel();
            }
        });
        _button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d(TAG, "Button 1 pressed");
            }
        });
        _button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d(TAG, "Button 2 pressed");
            }
        });

        return dialog;
    }
}

two_color_dialog.xml

<?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"
    android:orientation="vertical">

    <LinearLayout android:id="@+id/dialog_background"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@drawable/dialog_background">

        <LinearLayout android:id="@+id/dialog_title_spacing"
            android:layout_width="match_parent"
            android:layout_height="24dp"
            android:orientation="horizontal" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal">

            <Button android:id="@+id/dialog_button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="6dp"
                android:background="@drawable/button"
                android:text="Button1" />

            <Button android:id="@+id/dialog_button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="6dp"
                android:background="@drawable/button"
                android:text="Button2" />

        </LinearLayout>

    </LinearLayout>

    <TextView android:id="@+id/dialog_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|top"
        android:layout_marginTop="6dp"
        android:background="@drawable/dialog_title_box"
        android:textAppearance="@android:style/TextAppearance.Large"
        android:text="TITLE" />

    <ImageView android:id="@+id/dialog_close_button"
        android:layout_width="42dp"
        android:layout_height="44dp"
        android:layout_marginTop="5dp"
        android:layout_marginRight="5dp"
        android:layout_gravity="top|right"
        android:src="@drawable/dialog_close_button"/>

</FrameLayout>

dialog_title_box.xml

<?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="#88888888" />
            <corners android:radius="1dp" />
            <padding
                android:left="1dp"
                android:top="0dp"
                android:right="1dp"
                android:bottom="3dp" />
        </shape>
    </item>

    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/white" />
            <corners android:radius="1dp" />
            <padding
                android:left="15dp"
                android:top="5dp"
                android:right="15dp"
                android:bottom="5dp" />
        </shape>
    </item>
</layer-list>

dialog_close_button.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="oval">
            <solid android:color="#88888888" />
            <padding
                android:left="1dp"
                android:top="0dp"
                android:right="1dp"
                android:bottom="4dp" />
        </shape>
    </item>

    <item>
        <shape android:shape="oval">
            <solid android:color="@color/white" />
        </shape>
    </item>

    <item android:drawable="@drawable/ic_close" />
</layer-list>

回答1:


Well, I've found the issue. The problem only shows up when you use the "padding" tag. I have no idea why, maybe it is a bug. I was able to work around it by changing my dialog_close_button.xml accordingly:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="oval">
            <solid android:color="#88888888" />
        </shape>
    </item>

    <item
        android:left="1dp"
        android:right="1dp"
        android:bottom="4dp">
        <shape android:shape="oval">
            <solid android:color="@color/white" />
        </shape>
    </item>

    <item
        android:drawable="@drawable/ic_close"
        android:left="1dp"
        android:right="1dp"
        android:bottom="4dp" />
</layer-list>


来源:https://stackoverflow.com/questions/24643002/android-layer-drawable-loses-information

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!