安卓:图层绘制丢失信息 [英] Android: Layer drawable loses information

查看:250
本文介绍了安卓:图层绘制丢失信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与创造我自己的对话框类瞎搞,但我碰到一个有趣的问题。我使用的对话框的关闭按钮的图层列表可绘制,但由于某些原因,阴影层我第一次显示对话框后消失。

:您可以在下面的图片中看到

我真的完全不知道这可能是导致此。我试着调试,但我没能找到任何线索。没有任何人有什么想法?我的code如下。

TwoColorDialog.java

 公共类TwoColorDialog扩展DialogFragment {
    私有静态最后弦乐TAG =TwoColorDialog;    私人的ViewGroup _root;    私人TextView的_textTitle;
    私人ImageView的_buttonClose;
    私人按钮_button1;
    私人按钮_button2;    @覆盖
    公共对话框onCreateDialog(捆绑savedInstanceState){
        最后对话的对话=新的对话框(getActivity());
        。_root =(ViewGroup中)getActivity()getLayoutInflater()膨胀(R.layout.two_color_dialog,NULL);        _textTitle =(TextView中)_root.findViewById(R.id.dialog_title);
        _buttonClose =(ImageView的)_root.findViewById(R.id.dialog_close_button);
        _button1 =(按钮)_root.findViewById(R.id.dialog_button1);
        _button2 =(按钮)_root.findViewById(R.id.dialog_button2);        dialog.requestWindowFeature(STYLE_NO_TITLE);
        查看decorView = dialog.getWindow()getDecorView()。
        decorView.setBackgroundResource(getResources()的getColor(android.R.color.transparent));        dialog.setContentView(_root);        _buttonClose.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(查看视图){
                dialog.cancel();
            }
        });
        _button1.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(查看视图){
                Log.d(TAG按钮1 pressed);
            }
        });
        _button2.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(查看视图){
                Log.d(TAG按钮2 pressed);
            }
        });        返回对话框;
    }
}

two_color_dialog.xml

 <?XML版本=1.0编码=UTF-8&GT?;
<的FrameLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直>    <的LinearLayout机器人:ID =@ + ID / dialog_background
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:方向=垂直
        机器人:背景=@绘制/ dialog_background>        <的LinearLayout机器人:ID =@ + ID / dialog_title_spacing
            机器人:layout_width =match_parent
            机器人:layout_height =24dp
            机器人:方向=横向/>        <的LinearLayout
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:比重=中心
            机器人:方向=横向>            <按钮机器人:ID =@ + ID / dialog_button1
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_margin =6DP
                机器人:背景=@绘制/按钮
                机器人:文字=Button1的/>            <按钮机器人:ID =@ + ID / dialog_button2
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_margin =6DP
                机器人:背景=@绘制/按钮
                机器人:文字=Button2的/>        < / LinearLayout中>    < / LinearLayout中>    < TextView的机器人:ID =@ + ID / dialog_title
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_gravity =中心|顶
        机器人:layout_marginTop =6DP
        机器人:背景=@绘制/ dialog_title_box
        机器人:textAppearance =@安卓风格/ TextAppearance.Large
        机器人:文字=TITLE/>    < ImageView的机器人:ID =@ + ID / dialog_close_button
        机器人:layout_width =42dp
        机器人:layout_height =44dp
        机器人:layout_marginTop =5DP
        机器人:layout_marginRight =5DP
        机器人:layout_gravity =顶|权
        机器人:SRC =@绘制/ dialog_close_button/>< /&的FrameLayout GT;

dialog_title_box.xml

 <?XML版本=1.0编码=UTF-8&GT?;
<层列表的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目>
        <形状机器人:形状=矩形>
            [固体机器人:颜色=#88888888/>
            <角落的an​​droid:半径=1DP/>
            <填充
                机器人:左=1DP
                机器人:顶部=0dp
                机器人:右=1DP
                机器人:底部=3DP/>
        < /形状>
    < /项目>    <项目>
        <形状机器人:形状=矩形>
            [固体机器人:色=@色/白/>
            <角落的an​​droid:半径=1DP/>
            <填充
                机器人:左=15dp
                机器人:顶部=5DP
                机器人:右=15dp
                机器人:底部=5DP/>
        < /形状>
    < /项目>
< /层列表>

dialog_close_button.xml

 <?XML版本=1.0编码=UTF-8&GT?;
<层列表的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目>
        <形状机器人:形状=椭圆形>
            [固体机器人:颜色=#88888888/>
            <填充
                机器人:左=1DP
                机器人:顶部=0dp
                机器人:右=1DP
                机器人:底部=4DP/>
        < /形状>
    < /项目>    <项目>
        <形状机器人:形状=椭圆形>
            [固体机器人:色=@色/白/>
        < /形状>
    < /项目>    <项目机器人:可绘制=@绘制/ ic_close/>
< /层列表>


解决方案

好吧,我已经找到了问题。当您使用填充标签的问题仅出现。我不知道为什么,也许这是一个错误。我能够通过改变来解决它在我的 dialog_close_button.xml 的相应

 <?XML版本=1.0编码=UTF-8&GT?;
<层列表的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目>
        <形状机器人:形状=椭圆形>
            [固体机器人:颜色=#88888888/>
        < /形状>
    < /项目>    <项目
        机器人:左=1DP
        机器人:右=1DP
        机器人:底部=4DP>
        <形状机器人:形状=椭圆形>
            [固体机器人:色=@色/白/>
        < /形状>
    < /项目>    <项目
        机器人:可绘制=@绘制/ ic_close
        机器人:左=1DP
        机器人:右=1DP
        机器人:底部=4DP/>
< /层列表>

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>

解决方案

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>

这篇关于安卓:图层绘制丢失信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆