有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java安卓:Layer-drawable丢失信息

我正忙于创建自己的对话类,但我遇到了一个有趣的问题。我为对话框的关闭按钮使用了一个可绘制的图层列表,但由于某些原因,阴影层在我第一次显示对话框后消失。您可以在下图中看到:

enter image description here

我真的完全不知道是什么导致了这一切。我试过调试,但找不到任何线索。有人有什么想法吗?我的代码如下

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(安卓.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;
    }
}

双色对话框。xml

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

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

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

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

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

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

        </LinearLayout>

    </LinearLayout>

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

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

</FrameLayout>

对话框标题框。xml

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

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

对话框关闭按钮。xml

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

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

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

共 (1) 个答案

  1. # 1 楼答案

    我发现了问题所在。只有在使用“padding”标记时,问题才会出现。我不知道为什么,可能是个虫子。我可以通过更改对话框关闭按钮来解决这个问题。相应地,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" />
            </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>