如何在Android动态壁纸中更改对话框首选项的布局?

时间:2013-11-29 19:50:21

标签: android android-layout android-intent live-wallpaper

我正在尝试使用自定义对话框首选项,当膨胀时,每个选项都会调用一个Intent,并且膨胀的对话框具有自定义布局。我也试图在首选项本身上显示自定义xml布局。在选择之前首先在设置屏幕上显示的含义。我有一个自定义的xml布局,可以在左边显示文本,在右边显示4个图标图像。我希望每个图标都可以从那里或在对话框弹出窗口中选择。每个都有其意图。我知道这是可能的,因为我在动态壁纸中看到了它。我理解如何调用一个Intent并应用一个自定义布局,但是我尝试了我得到错误“无法膨胀对话框”。任何帮助将不胜感激。您可以在此处查看我的代码:TestLWP Settings

以下是我想要实现的目标的图像:Preview1

enter image description here

1 个答案:

答案 0 :(得分:1)

我已经弄清楚如何实现上述两个图像。我在这里为所有想要使用它的人提供代码。我还详细评论了代码,以解释发生了什么。

首先创建一个扩展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”:

            <LinearLayout 
        android:gravity="center" 
        android:orientation="horizontal" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
      xmlns:android="http://schemas.android.com/apk/res/android">
        <ImageView 
            android:layout_gravity="center" 
            android:id="@+id/iv_social_ytube" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_margin="@dimen/value_margin_half" 
            android:src="@drawable/selector_ytube" />
        <ImageView 
            android:layout_gravity="center" 
            android:id="@+id/iv_social_gplus" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_margin="@dimen/value_margin_half" 
            android:src="@drawable/selector_gplus" />
        <ImageView 
            android:layout_gravity="center" 
            android:id="@+id/iv_social_facebook" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_margin="@dimen/value_margin_half" 
            android:src="@drawable/selector_fb" />
        <ImageView 
            android:layout_gravity="center" 
            android:id="@+id/iv_social_twitter" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_margin="@dimen/value_margin_half" 
            android:src="@drawable/selector_twitter" />
    </LinearLayout>

此xml是Dialog的布局,将其命名为“pref_social_dialog.xml”:

            <LinearLayout android:gravity="left" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="@dimen/value_margin_half"
      xmlns:android="http://schemas.android.com/apk/res/android">
        <TextView android:gravity="left|center" android:id="@+id/tv_social_ytube" android:clickable="true" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="@dimen/value_margin_half" android:text="@string/Social_YouTube" android:drawableLeft="@drawable/selector_ytube" android:drawablePadding="@dimen/value_margin" />
        <View android:background="?android:listDivider" android:layout_width="fill_parent" android:layout_height="1.0dip" />
        <TextView android:gravity="left|center" android:id="@+id/tv_social_gplus" android:clickable="true" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="@dimen/value_margin_half" android:text="@string/Social_GooglePlus" android:drawableLeft="@drawable/selector_gplus" android:drawablePadding="@dimen/value_margin" />
        <View android:background="?android:listDivider" android:layout_width="fill_parent" android:layout_height="1.0dip" />
        <TextView android:gravity="left|center" android:id="@+id/tv_social_facebook" android:clickable="true" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="@dimen/value_margin_half" android:text="@string/Social_Facebook" android:drawableLeft="@drawable/selector_fb" android:drawablePadding="@dimen/value_margin" />
        <View android:background="?android:listDivider" android:layout_width="fill_parent" android:layout_height="1.0dip" />
        <TextView android:gravity="left|center" android:id="@+id/tv_social_twitter" android:clickable="true" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="@dimen/value_margin_half" android:text="@string/Social_Twitter" android:drawableLeft="@drawable/selector_twitter" android:drawablePadding="@dimen/value_margin" />
        <View android:background="?android:listDivider" android:layout_width="fill_parent" android:layout_height="1.0dip" />
    </LinearLayout>

为drawable文件夹中的图标创建一个xml文件。这将在选择时更改图标。称之为“selector_fb.xml”为YouTube,Twitter和GPlus创建相同的内容:“selector_ytube.xml”,“selector_twitter.xml”,“selector_gplus.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>

您必须使用自己的图片。只需确保所选的一个是灰度,并且所有图像的大小都适合每​​种尺寸的设备。希望这有助于其他人。