Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何更改Android live墙纸中对话框首选项的布局?_Android_Android Layout_Android Intent_Live Wallpaper - Fatal编程技术网

如何更改Android live墙纸中对话框首选项的布局?

如何更改Android live墙纸中对话框首选项的布局?,android,android-layout,android-intent,live-wallpaper,Android,Android Layout,Android Intent,Live Wallpaper,我试图有一个自定义对话框首选项,当膨胀每个选择调用一个意图和膨胀的对话框有一个自定义布局。我还试图在首选项本身上显示自定义xml布局。这意味着它在被选择之前第一次显示在设置屏幕上。我有一个自定义的xml布局,左侧显示文本,右侧显示4个图标图像。我希望每个图标都可以从那里或在弹出的对话框中选择。每个人都有自己的意图。我知道这是可能的,因为我在一张活的壁纸中看到了它。我知道如何调用Intent并应用自定义布局,但通过我的尝试,我得到了错误“无法膨胀对话框”。任何帮助都将不胜感激。您可以在此处查看我的

我试图有一个自定义对话框首选项,当膨胀每个选择调用一个意图和膨胀的对话框有一个自定义布局。我还试图在首选项本身上显示自定义xml布局。这意味着它在被选择之前第一次显示在设置屏幕上。我有一个自定义的xml布局,左侧显示文本,右侧显示4个图标图像。我希望每个图标都可以从那里或在弹出的对话框中选择。每个人都有自己的意图。我知道这是可能的,因为我在一张活的壁纸中看到了它。我知道如何调用Intent并应用自定义布局,但通过我的尝试,我得到了错误“无法膨胀对话框”。任何帮助都将不胜感激。您可以在此处查看我的代码:

以下是我试图实现的目标:


我已经想出了如何实现上述两个图像。我在这里为任何想使用它的人提供代码。我还对代码进行了详细的注释,以解释正在发生的事情

首先创建一个扩展DialogPreference的类,并添加以下内容:

        import android.app.Dialog;
        import android.content.Context;
        import android.content.Intent;
        import android.net.Uri;
        import android.preference.DialogPreference;
        import android.util.AttributeSet;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.view.ViewGroup;

        public class SocialPref extends DialogPreference implements OnClickListener
        {




          public SocialPref(Context paramContext, AttributeSet paramChar)
          {
            super(paramContext, paramChar);
            //Use setWidgetLayoutResource to display the setting how you
            //want it in the settings screen
            setWidgetLayoutResource(R.layout.pref_social);
            //Set the title of the setting
            setTitle(R.string.SocialTitle);
            //Set the summary for the setting
            setSummary(R.string.SocialSummary);
            //Here you set the title for the Dialog
            setDialogTitle(R.string.SocialTitle);
            //Here you declare the layout for the Dialog
            setDialogLayoutResource(R.layout.pref_social_dialog);
            setNegativeButtonText(R.string.Cancel);
          }



          //Void a,b,c,and d are called when each icon is pressed
          //on the setting or when each list item is selected within the dialog.
          //allowing the user to select each social site from the setting or the dialog.
          //each action includes an intent that launches the corresponding social site.
          private void a(View paramView, int paramInt)
          {
            if (paramView != null)
            {
              View localView = paramView.findViewById(paramInt);
              if (localView != null)
                localView.setOnClickListener(new View.OnClickListener()
                {
                    public final void onClick(View paramView)
                      {
                        this.gPlus();
                      }

                    private void gPlus()
                      {
                        Dialog localDialog = getDialog();
                        if (localDialog != null)
                          localDialog.dismiss();
                        Context localContext = getContext();
                        if (localContext != null)
                        {
                          Intent localIntent = new Intent();
                          localIntent.setAction("android.intent.action.VIEW");
                          //Important: put your G+ page where it says "gplus_account_here" or it won't work
                          localIntent.setData(Uri.parse("https://plus.google.com/gplus_account_here"));
                          localContext.startActivity(localIntent);
                        }
                      }
                });
            }
          }

          private void b(View paramView, int paramInt)
          {
            if (paramView != null)
            {
              View localView = paramView.findViewById(paramInt);
              if (localView != null)
                localView.setOnClickListener(new View.OnClickListener()
                {

                    @Override
                    public void onClick(View v) {
                        this.faceBook();

                    }
                    private void faceBook()
                      {
                        Dialog localDialog = getDialog();
                        if (localDialog != null)
                          localDialog.dismiss();
                        Context localContext = getContext();
                        if (localContext != null)
                        {
                          Intent localIntent1 = new Intent();
                          localIntent1.setAction("android.intent.action.VIEW");
                          //Important: put your FaceBook id and page in their respective locations
                          //or it won't work.
                          localIntent1.setData(Uri.parse("fb://profile/profile_id"));
                          Intent localIntent2 = ac.a(localContext, localIntent1, false);
                          if (localIntent2 == null)
                          {
                            localIntent2 = new Intent();
                            localIntent2.setAction("android.intent.action.VIEW");
                            localIntent2.setData(Uri.parse("https://facebook.com/profile_page"));
                          }
                          localContext.startActivity(localIntent2);
                        }
                      }



              });
          }
        }

          private void c(View paramView, int paramInt)
          {
            if (paramView != null)
            {
              View localView = paramView.findViewById(paramInt);
              if (localView != null)
                localView.setOnClickListener(new View.OnClickListener()
                {

                    @Override
                    public void onClick(View v) {
                        this.twitter();
                    }
                    private void twitter()
                      {
                        Dialog localDialog = getDialog();
                        if (localDialog != null)
                          localDialog.dismiss();
                        Context localContext = getContext();
                        if (localContext != null)
                        {
                          Intent localIntent1 = new Intent();
                          localIntent1.setAction("android.intent.action.VIEW");
                          //Important: put your Twitter screen name where it says
                          //"screen_name_here" or it won't work
                          localIntent1.setData(Uri.parse("twitter://user?screen_name=screen_name_here"));
                          Intent localIntent2 = ac.a(localContext, localIntent1, false);
                          if (localIntent2 == null)
                          {
                            localIntent2 = new Intent();
                            localIntent2.setAction("android.intent.action.VIEW");
                            localIntent2.setData(Uri.parse("https://twitter.com/intent/user?screen_name=screen_name_here"));
                          }
                          localContext.startActivity(localIntent2);
                        }
                      }
                });
            }
          }

          private void d(View paramView, int paramInt)
          {
            if (paramView != null)
            {
                View localView = paramView.findViewById(paramInt);
                if (localView != null)
                  localView.setOnClickListener(new View.OnClickListener()
                  {

                    @Override
                    public void onClick(View paramView) {
                        this.youTube();
                    }
                    private void youTube()
                      {
                        Dialog localDialog = getDialog();
                        if (localDialog != null)
                          localDialog.dismiss();
                        Context localContext = getContext();
                        if (localContext != null)
                        {
                          Intent localIntent = new Intent();
                          localIntent.setAction("android.intent.action.VIEW");
                          //Important: Put your YouTube channel name where it 
                          //says "username_here" or it won't work
                          localIntent.setData(Uri.parse("http://www.youtube.com/user/username_here"));
                          localContext.startActivity(localIntent);
                        }
                      }

                  });
            }
          }


        //This instantiates the view for the dialog
          //and allows each icon to be clickable
          protected final View onCreateDialogView()
          {
            View localView = super.onCreateDialogView();
            //For example: when a(localView, d.tv_social_gplus); is
            //called, public void a(); is started taking the user
            //to the related social site or app
            a(localView, d.tv_social_gplus);
            b(localView, d.tv_social_facebook);
            c(localView, d.tv_social_twitter);
            d(localView, d.tv_social_ytube);
            return localView;
          }

          //This instantiates the view for the setting within the setting
          //screen and allows each icon to be clicked, invoking
          //the corresponding action.
          protected final View onCreateView(ViewGroup paramViewGroup)
          {
            View localView = super.onCreateView(paramViewGroup);
            a(localView, d.iv_social_gplus);
            b(localView, d.iv_social_facebook);
            c(localView, d.iv_social_twitter);
            d(localView, d.iv_social_ytube);
            return localView;
          }

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }


        }
然后创建一个名为“d”的空类来存储整数:

                public final class d
        {


          public static final int iv_social_facebook = R.id.iv_social_facebook;
          public static final int iv_social_gplus = R.id.iv_social_gplus;
          public static final int iv_social_twitter = R.id.iv_social_twitter;
          public static final int iv_social_ytube = R.id.iv_social_ytube;

          public static final int tv_social_facebook = R.id.tv_social_facebook;
          public static final int tv_social_gplus = R.id.tv_social_gplus;
          public static final int tv_social_twitter = R.id.tv_social_twitter;
          public static final int tv_social_ytube = R.id.tv_social_ytube;

        }
这是设置的xml,将其命名为“pref_social”:


此xml是对话框的布局,将其命名为“pref_social_Dialog.xml”:


为drawable文件夹中的图标创建xml文件。这将在选中时更改图标。将其命名为“selector_fb.xml”,为YouTube、Twitter和GPlus创建相同的名称,如下所示:“selector_ytube.xml”、“selector_Twitter.xml”、“selector_GPlus.xml”



你必须使用自己的图像。只需确保所选的图像是灰度的,并且所有图像的大小都适合每种大小的设备。希望这对其他人有所帮助。

我已经想出了如何实现上述两个图像。我在这里为任何想使用它的人提供代码。我还对代码进行了详细的注释,以解释正在发生的事情

首先创建一个扩展DialogPreference的类,并添加以下内容:

        import android.app.Dialog;
        import android.content.Context;
        import android.content.Intent;
        import android.net.Uri;
        import android.preference.DialogPreference;
        import android.util.AttributeSet;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.view.ViewGroup;

        public class SocialPref extends DialogPreference implements OnClickListener
        {




          public SocialPref(Context paramContext, AttributeSet paramChar)
          {
            super(paramContext, paramChar);
            //Use setWidgetLayoutResource to display the setting how you
            //want it in the settings screen
            setWidgetLayoutResource(R.layout.pref_social);
            //Set the title of the setting
            setTitle(R.string.SocialTitle);
            //Set the summary for the setting
            setSummary(R.string.SocialSummary);
            //Here you set the title for the Dialog
            setDialogTitle(R.string.SocialTitle);
            //Here you declare the layout for the Dialog
            setDialogLayoutResource(R.layout.pref_social_dialog);
            setNegativeButtonText(R.string.Cancel);
          }



          //Void a,b,c,and d are called when each icon is pressed
          //on the setting or when each list item is selected within the dialog.
          //allowing the user to select each social site from the setting or the dialog.
          //each action includes an intent that launches the corresponding social site.
          private void a(View paramView, int paramInt)
          {
            if (paramView != null)
            {
              View localView = paramView.findViewById(paramInt);
              if (localView != null)
                localView.setOnClickListener(new View.OnClickListener()
                {
                    public final void onClick(View paramView)
                      {
                        this.gPlus();
                      }

                    private void gPlus()
                      {
                        Dialog localDialog = getDialog();
                        if (localDialog != null)
                          localDialog.dismiss();
                        Context localContext = getContext();
                        if (localContext != null)
                        {
                          Intent localIntent = new Intent();
                          localIntent.setAction("android.intent.action.VIEW");
                          //Important: put your G+ page where it says "gplus_account_here" or it won't work
                          localIntent.setData(Uri.parse("https://plus.google.com/gplus_account_here"));
                          localContext.startActivity(localIntent);
                        }
                      }
                });
            }
          }

          private void b(View paramView, int paramInt)
          {
            if (paramView != null)
            {
              View localView = paramView.findViewById(paramInt);
              if (localView != null)
                localView.setOnClickListener(new View.OnClickListener()
                {

                    @Override
                    public void onClick(View v) {
                        this.faceBook();

                    }
                    private void faceBook()
                      {
                        Dialog localDialog = getDialog();
                        if (localDialog != null)
                          localDialog.dismiss();
                        Context localContext = getContext();
                        if (localContext != null)
                        {
                          Intent localIntent1 = new Intent();
                          localIntent1.setAction("android.intent.action.VIEW");
                          //Important: put your FaceBook id and page in their respective locations
                          //or it won't work.
                          localIntent1.setData(Uri.parse("fb://profile/profile_id"));
                          Intent localIntent2 = ac.a(localContext, localIntent1, false);
                          if (localIntent2 == null)
                          {
                            localIntent2 = new Intent();
                            localIntent2.setAction("android.intent.action.VIEW");
                            localIntent2.setData(Uri.parse("https://facebook.com/profile_page"));
                          }
                          localContext.startActivity(localIntent2);
                        }
                      }



              });
          }
        }

          private void c(View paramView, int paramInt)
          {
            if (paramView != null)
            {
              View localView = paramView.findViewById(paramInt);
              if (localView != null)
                localView.setOnClickListener(new View.OnClickListener()
                {

                    @Override
                    public void onClick(View v) {
                        this.twitter();
                    }
                    private void twitter()
                      {
                        Dialog localDialog = getDialog();
                        if (localDialog != null)
                          localDialog.dismiss();
                        Context localContext = getContext();
                        if (localContext != null)
                        {
                          Intent localIntent1 = new Intent();
                          localIntent1.setAction("android.intent.action.VIEW");
                          //Important: put your Twitter screen name where it says
                          //"screen_name_here" or it won't work
                          localIntent1.setData(Uri.parse("twitter://user?screen_name=screen_name_here"));
                          Intent localIntent2 = ac.a(localContext, localIntent1, false);
                          if (localIntent2 == null)
                          {
                            localIntent2 = new Intent();
                            localIntent2.setAction("android.intent.action.VIEW");
                            localIntent2.setData(Uri.parse("https://twitter.com/intent/user?screen_name=screen_name_here"));
                          }
                          localContext.startActivity(localIntent2);
                        }
                      }
                });
            }
          }

          private void d(View paramView, int paramInt)
          {
            if (paramView != null)
            {
                View localView = paramView.findViewById(paramInt);
                if (localView != null)
                  localView.setOnClickListener(new View.OnClickListener()
                  {

                    @Override
                    public void onClick(View paramView) {
                        this.youTube();
                    }
                    private void youTube()
                      {
                        Dialog localDialog = getDialog();
                        if (localDialog != null)
                          localDialog.dismiss();
                        Context localContext = getContext();
                        if (localContext != null)
                        {
                          Intent localIntent = new Intent();
                          localIntent.setAction("android.intent.action.VIEW");
                          //Important: Put your YouTube channel name where it 
                          //says "username_here" or it won't work
                          localIntent.setData(Uri.parse("http://www.youtube.com/user/username_here"));
                          localContext.startActivity(localIntent);
                        }
                      }

                  });
            }
          }


        //This instantiates the view for the dialog
          //and allows each icon to be clickable
          protected final View onCreateDialogView()
          {
            View localView = super.onCreateDialogView();
            //For example: when a(localView, d.tv_social_gplus); is
            //called, public void a(); is started taking the user
            //to the related social site or app
            a(localView, d.tv_social_gplus);
            b(localView, d.tv_social_facebook);
            c(localView, d.tv_social_twitter);
            d(localView, d.tv_social_ytube);
            return localView;
          }

          //This instantiates the view for the setting within the setting
          //screen and allows each icon to be clicked, invoking
          //the corresponding action.
          protected final View onCreateView(ViewGroup paramViewGroup)
          {
            View localView = super.onCreateView(paramViewGroup);
            a(localView, d.iv_social_gplus);
            b(localView, d.iv_social_facebook);
            c(localView, d.iv_social_twitter);
            d(localView, d.iv_social_ytube);
            return localView;
          }

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }


        }
然后创建一个名为“d”的空类来存储整数:

                public final class d
        {


          public static final int iv_social_facebook = R.id.iv_social_facebook;
          public static final int iv_social_gplus = R.id.iv_social_gplus;
          public static final int iv_social_twitter = R.id.iv_social_twitter;
          public static final int iv_social_ytube = R.id.iv_social_ytube;

          public static final int tv_social_facebook = R.id.tv_social_facebook;
          public static final int tv_social_gplus = R.id.tv_social_gplus;
          public static final int tv_social_twitter = R.id.tv_social_twitter;
          public static final int tv_social_ytube = R.id.tv_social_ytube;

        }
这是设置的xml,将其命名为“pref_social”:


此xml是对话框的布局,将其命名为“pref_social_Dialog.xml”:


为drawable文件夹中的图标创建xml文件。这将在选中时更改图标。将其命名为“selector_fb.xml”,为YouTube、Twitter和GPlus创建相同的名称,如下所示:“selector_ytube.xml”、“selector_Twitter.xml”、“selector_GPlus.xml”



你必须使用自己的图像。只需确保所选的图像是灰度的,并且所有图像的大小都适合每种大小的设备。希望这对其他人有所帮助。

我想你可以使用一个
列表来显示你的信息,但第一张图片只有4个并排的图片和两个或三个
textview
,我对你的工具说得对吗?请发布一些你已经尝试过的代码,并提出具体问题以获得具体答案。您在实现自定义首选项布局时遇到了问题吗?实现自定义对话框布局是否有问题?单击首选项打开对话框时有问题吗?我所有的代码都在提供的github链接中。我尝试将自定义对话框首选项添加到我的设置xml中。实时壁纸加载了,但当我点击设置时,它崩溃了,logcat说“充气对话框失败。这就是我被卡住的地方,因为在选择特定设置之前,它不应该充气。我定制的seekbar偏好没有做到这一点。设置屏幕和对话框的xml布局看起来都像预期的那样。我只需要设置屏幕来显示它应该的方式,然后我就可以计算出其余的。解决了我的问题。对于任何想使用此对话框作为自己的应用程序/实时壁纸首选项的人,代码都包括在内。我认为您可以使用一个
列表
来显示您的信息,但第一个图像只有4个并排的图像和两个或三个
文本视图
,关于你的工具我说的对吗?请发布一些你已经尝试过的代码,并提出具体的问题以得到具体的答案。您在实现自定义首选项布局时遇到了问题吗?实现自定义对话框布局是否有问题?单击首选项打开对话框时有问题吗?我所有的代码都在提供的github链接中。我尝试将自定义对话框首选项添加到我的设置xml中。实时壁纸加载了,但当我点击设置时,它崩溃了,logcat说“充气对话框失败。这就是我被卡住的地方,因为在选择特定设置之前,它不应该充气。我定制的seekbar偏好没有做到这一点。设置屏幕和对话框的xml布局看起来都像预期的那样。我只需要设置屏幕来显示它应该的方式,然后我就可以计算出其余的。解决了我的问题。对于希望将此对话框首选项用于自己的应用程序/实时墙纸的任何人,包含此代码。
            <selector xmlns:android="http://schemas.android.com/apk/res/android" >
        <item android:state_pressed="false" android:drawable="@drawable/fb" />
        <item android:state_pressed="true" android:drawable="@drawable/fb_pressed" />

    </selector>