开发者

Always show scrollbars for PreferenceScreen

开发者 https://www.devze.com 2023-01-28 09:17 出处:网络
I have defined preferences in my app using preferences.xml and a PreferenceActivity. The settings are presented in the phone in a ScrollView-like-way. I would like to always show the vertical scrollba

I have defined preferences in my app using preferences.xml and a PreferenceActivity. The settings are presented in the phone in a ScrollView-like-way. I would like to always show the vertical scrollbars all the time. In Android 1.6 they do not fade away, but in Android 2.2 the fade away after about a second. In a ScrollView I can control this using android:sc开发者_开发知识库rollbarAlwaysDrawVerticalTrack or android:scrollbarDefaultDelayBeforeFade. How can I do this with the preference widget?

/P


This worked for my Android 2.3.3 .

public class MyPreferenceActivity extends PreferenceActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getListView().setScrollbarFadingEnabled(false);
        ...
    }
    ...
}


Try this as the above suggestions didn't work for me when I wanted to do this for a TextView:

TextView.setScrollbarFadingEnabled(false);


Try android:scrollbarAlwaysDrawVerticalTrack="true" in the PreferenceScreen.


So hey, this is a seriously ugly solution, but its short and it works.

public class PreferenceActivity extends Activity {
//Sorry for stupid variable names, couldn't be bothered to be smart
    private ScrollView svOptions;
    private Runnable _run_sb_on;
    private final Handler _handle_sb_on = new Handler(); 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.preference);

        svOptions = (ScrollView) findViewById(R.id.svOptions);
        _run_sb_on = new Runnable() { //i hate you google for making me write crap code ! 
            public void run() {
                svOptions.fling(0); //don't move it, just keep it alive
                _handle_sb_on.postDelayed(this, 300); //300 is the timeout of the fader 
            }
        }; 
        _handle_sb_on.post(_run_sb_on);
    }

    //  etc

}

So there you have it. Note that the fling can eat on-touch events, its rare but its a little annoying - but workable.


Put this in the scroller that you want to show scrollbars all the time:

android:scrollbarFadeDuration="0"

Counterintuitive, I know, but it works perfectly.

0

精彩评论

暂无评论...
验证码 换一张
取 消