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

后端 未结 5 1134
既然无缘
既然无缘 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: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

提交回复
热议问题