开发者

How to set "android:scrollbars=vertical" programmatically?

I want to dynamically set scrollbars in an EditText programm开发者_StackOverflow社区atically, but I don't find this api as setscrollbars. Could anyone tell me if this can be done programmatically?

android:scrollbars="vertical"

Is this possible? How can I achieve this programmatically?


I was looking for a method to do this programatically, too.

Seems from http://developer.android.com/reference/android/view/View.html#attr_android:scrollbars it's not possible, cause no related method is mentioned there.

The only method that sounds good by description is:

View.setVerticalScrollBarEnabled(boolean)

but in my case it didn't work. But that may depend on the layout and the way it is used.

I am thinking now about having two layouts: one with the attribute set to "none" and one set to "vertical". Then in code I could decide which to use.


    final TypedArray typedArray = context.obtainStyledAttributes(null,  new int[]{});
            try {
                Method method = View.class.getDeclaredMethod("initializeScrollbars" , TypedArray.class);
                method.invoke(this, typedArray);
            }catch (Exception e) {
            }
            typedArray.recycle();
 setVerticalScrollBarEnabled(true);


You have to override the android:scrollbarTrackVertical and android:scrollbarThumbVertical drawables with your own. You can do it in code or with styling.

This is a link that shows how to do custom buttons. The principle for scrollbars is similar.


Programmatically :

RecyclerView mRecyclerView =rootView.findViewById(R.id.your-recycler-view);
if(mRecyclerView!=null){
  mRecyclerView.setVerticalScrollBarEnabled( true );
}

In the XML :

<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:scrollbars="vertical"
android:layout_height="wrap_content" />

Other scrollbars:

android:scrollbars="vertical"
android:scrollbars="horizontal"
android:scrollbars="vertical|horizontal"
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜