Android:图层可绘制丢失信息

Posted

技术标签:

【中文标题】Android:图层可绘制丢失信息【英文标题】:Android: Layer drawable loses information 【发布时间】:2014-08-29 20:21:58 【问题描述】:

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

我真的完全不知道是什么原因造成的。我试过调试,但我找不到任何线索。有没有人有任何想法?我的代码如下。

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

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

        <LinearLayout android:id="@+id/dialog_title_spacing"
            android:layout_
            android:layout_
            android:orientation="horizontal" />

        <LinearLayout
            android:layout_
            android:layout_
            android:gravity="center"
            android:orientation="horizontal">

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

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

        </LinearLayout>

    </LinearLayout>

    <TextView android:id="@+id/dialog_title"
        android:layout_
        android:layout_
        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_
        android:layout_
        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>

【问题讨论】:

真是一团糟!嵌套布局太多(最好只使用 1RelativeLayout)和太多层列表(只需 1 就足够了)! 【参考方案1】:

好吧,我找到了问题所在。该问题仅在您使用“填充”标签时出现。我不知道为什么,也许这是一个错误。我能够通过相应地更改我的 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" />
        </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>

【讨论】:

以上是关于Android:图层可绘制丢失信息的主要内容,如果未能解决你的问题,请参考以下文章

如何防止android层可绘制形状(例如圆形)缩放

Android自定义View(3) 《Canvas绘制简单的图形》

如何在按钮中创建简单的可绘制图层

ArcGis基础——设置图层可选状态

arcgis for android(10.2.9)- 点线面绘制前奏

调整可绘制图层列表内的位图大小 2