Custom preference, targetSdkVersion=“11”: missing indent?

后端 未结 5 1135
既然无缘
既然无缘 2020-12-14 18:49

I have a couple of custom DialogPreference implementations floating around, such as this one:

package apt.tutorial;

import android.content.Cont         


        
相关标签:
5条回答
  • 2020-12-14 19:15

    The solution which helped me:

    I have replaced

    public TimePreference(Context ctxt, AttributeSet attrs) {
        this(ctxt, attrs, 0);
    }
    

    with

    public TimePreference(Context ctxt, AttributeSet attrs) {
        this(ctxt, attrs, ctxt.getResources().getSystem().getIdentifier("dialogPreferenceStyle", "attr", "android"));
    }
    

    As you can see, I replaced third argument 0 with ctxt.getResources().getSystem().getIdentifier("dialogPreferenceStyle", "attr", "android") in the second constructor of custom preference class.

    0 讨论(0)
  • 2020-12-14 19:20

    You can dance with void Preference.setWidgetLayoutResource(int widgetLayoutResId) method, although I prefer to override View Preference.onCreateView(ViewGroup parent) method in my custom Preference class and hack it by adding custom views just below @android:id/summary (use hierarchyviewer utility for details).

    The complete method is:

    @Override
    protected View onCreateView(ViewGroup parent)
    {
        View ret = super.onCreateView(parent);
    
        View summary = ret.findViewById(android.R.id.summary);
        if (summary != null)
        {
            ViewParent summaryParent = summary.getParent();
            if (summaryParent instanceof ViewGroup)
            {
                final LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                ViewGroup summaryParent2 = (ViewGroup) summaryParent;
                layoutInflater.inflate(R.layout.seek_bar_preference, summaryParent2);
    
                seekBar = (SeekBar) summaryParent2.findViewById(R.id.seekBar);
                seekBar.setMax(maxValue - minValue);
                seekBar.setOnSeekBarChangeListener(this);
    
                statusText = (TextView) summaryParent2.findViewById(R.id.seekBarPrefValue);
    
                unitsRightView = (TextView) summaryParent2.findViewById(R.id.seekBarPrefUnitsRight);
                unitsLeftView = (TextView) summaryParent2.findViewById(R.id.seekBarPrefUnitsLeft);
            }
        }
    
        return ret;
    }
    

    Source code of my SeekBarPreference class based on code from http://robobunny.com can be downloaded here image1 image2

    0 讨论(0)
  • 2020-12-14 19:21

    I tried your code on the emulator. There is no problem with the code that you have given, and all the lines have the same formatting; but they all look more similar (in format) to the third preference (Lunch Alarm Time) than the others.

    It looks like the other three preferences are getting indented more than required. So, maybe you have some global formatting style that is used, but not picked up by the TimePreference preference.

    EDIT: OK. So, the above is not (completely) true. There is definitely a problem when I tried with the target sdk set to HoneyComb. But on setting the theme for the PreferenceActivity class as android:theme="@android:style/Theme.Black", there is a consistency in the look of all the preferences as shown below.

    enter image description here

    This style looks similar to Froyo, but not the HoneyComb; in the latter, the title font is smaller and there is more indentation. Probably, the default theme is not being assigned to Custom Preferences - just a guess :) A workaround would be to assign the default theme to your preference activity explicitly, but I don't know what the default theme in HoneyComb is (and whether it can be set).

    0 讨论(0)
  • 2020-12-14 19:27

    To make the accepted answer more clear. You only need this constructor:

    public TimePreference(Context ctxt, AttributeSet attrs) {
        // this(ctxt, attrs, 0); // wrong
        super(ctxt, attrs); 
    }
    
    0 讨论(0)
  • 2020-12-14 19:33

    (cross-posting from the associated android-developers thread)

    OK, I figured it out.

    There are three possible constructors on a Preference:

    MyPreference(Context ctxt)
    MyPreference(Context ctxt, AttributeSet attrs)
    MyPreference(Context ctxt, AttributeSet attrs, int defStyle)
    

    Somewhere along the line, I picked up the pattern of having the one-parameter constructor chain to the two-parameter constructor (passing null for the 2nd parameter), and having the two-parameter constructor chain to the three-parameter constructor (passing 0 for the 3rd parameter).

    And that's not the right answer.

    I am hoping that the right answer is to only implement the second constructor, because the correct default style is internal to Android (com.android.internal.R.attr.dialogPreferenceStyle). The second constructor is the one used with inflating preference XML.

    Thanks to all for the help!

    0 讨论(0)
提交回复
热议问题