安卓:启用自定义视图滚动条自定义、视图、滚动条

2023-09-06 03:06:21 作者:太过爱你,以至于失去自己

我实现了自定义布局,扩展RelativeLayout的。它显示拍创建的不同的元素在运行时的和是可滚动使用scrollTo()和scrollBy()这两个方面。滚动的作品,现在我想补充的标准Android滚动条。

I have implemented a custom Layout that extends RelativeLayout. It displays lots of different elements that are created at run-time and is scrollable in both dimensions using scrollTo() and scrollBy(). Scrolling works and now I'd like to add the standard Android scrollbars.

使用Scrollviews北京时间不可能的,因为我需要布局中滚动2维,所以我尝试做如下描述:Android:启用基于canvas的视图滚动条

Using Scrollviews ist not possible, since I need the Layout to be scrollable in 2 dimensions, so I tried to do it as described here: Android: Enable Scrollbars on Canvas-Based View

我已经实现了所有的计算*方法与一些(假的)值并启用滚动条。但我还是不能让他们露面。任何想法可能是什么问题?

I have implemented all the compute* methods with some (bogus) values and enabled the scrollbars. But I still can't get them to show up. Any ideas what might be the problem?

有吨这样的问题在不同的邮件列表和SO,但随处可见的答案似乎是一呼setHorizo​​ntalScrollbarEnabled(真),2。实现所有的计算*方法,3.调用awakenScrollbars() 。至于我可以告诉大家,我已经做了这一切,甚至试图用initializeScrollbars(),但没有任何反应和文档不提供任何帮助。

There are tons of questions like this in various mailing lists and on SO, but everywhere the answer seems to be "1. call setHorizontalScrollbarEnabled(true), 2. implement all the compute* methods, 3. call awakenScrollbars()". As far as I can tell, I have done all of that and even tried to use initializeScrollbars() but nothing happens and the docs don't offer any help.

public NodeLayout(Context context) {
    super(context);

    setVerticalScrollBarEnabled(true);
    setHorizontalScrollBarEnabled(true);

    TypedArray a = context.obtainStyledAttributes(R.styleable.View);
    initializeScrollbars(a);
    a.recycle();
}

@Override
protected int computeHorizontalScrollExtent() {
    return 5;
}

@Override
protected int computeHorizontalScrollOffset() {
    return 10;
}

@Override
protected int computeHorizontalScrollRange() {
    return 50;
}

@Override
protected int computeVerticalScrollExtent() {
    return getHeight() / 2;
}

@Override
protected int computeVerticalScrollOffset() {
    return getHeight() / 2;
}

@Override
protected int computeVerticalScrollRange() {
    return getHeight();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    awakenScrollBars();
    invalidate();
    return true;
}

和这是我的attrs.xml文件看起来像:

And this is how my attrs.xml file looks like:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="View">
<attr name="android:background"/>
<attr name="android:clickable"/>
<attr name="android:contentDescription"/>
<attr name="android:drawingCacheQuality"/>
<attr name="android:duplicateParentState"/>
<attr name="android:fadeScrollbars"/>
<attr name="android:fadingEdge"/>
<attr name="android:fadingEdgeLength"/>
<attr name="android:fitsSystemWindows"/>
<attr name="android:focusable"/>
<attr name="android:focusableInTouchMode"/>
<attr name="android:hapticFeedbackEnabled"/>
<attr name="android:id"/>
<attr name="android:isScrollContainer"/>
<attr name="android:keepScreenOn"/>
<attr name="android:longClickable"/>
<attr name="android:minHeight"/>
<attr name="android:minWidth"/>
<attr name="android:nextFocusDown"/>
<attr name="android:nextFocusLeft"/>
<attr name="android:nextFocusRight"/>
<attr name="android:nextFocusUp"/>
<attr name="android:onClick"/>
<attr name="android:padding"/>
<attr name="android:paddingBottom"/>
<attr name="android:paddingLeft"/>
<attr name="android:paddingRight"/>
<attr name="android:paddingTop"/>
<attr name="android:saveEnabled"/>
<attr name="android:scrollX"/>
<attr name="android:scrollY"/>
<attr name="android:scrollbarAlwaysDrawHorizontalTrack"/>
<attr name="android:scrollbarAlwaysDrawVerticalTrack"/>
<attr name="android:scrollbarDefaultDelayBeforeFade"/>
<attr name="android:scrollbarFadeDuration"/>
<attr name="android:scrollbarSize"/>
<attr name="android:scrollbarStyle"/>
<attr name="android:scrollbarThumbHorizontal"/>
<attr name="android:scrollbarThumbVertical"/>
<attr name="android:scrollbarTrackHorizontal"/>
<attr name="android:scrollbarTrackVertical"/>
<attr name="android:scrollbars"/>
<attr name="android:soundEffectsEnabled"/>
<attr name="android:tag"/>
<attr name="android:visibility"/>
</declare-styleable>
</resources>

我开发的Andr​​oid 3.0。

I am developing for Android 3.0.

推荐答案

只是有这个同样的问题,终于搞明白了。

Just had this same problem and finally figured it out.

问题是,要扩展 RelativeLayout的,这是的ViewGroup 的子类。我发现,我没有问题,做什么,你在你的问题中所述,以得到一个自定义查看来显示滚动条,但是当我伸出的ViewGroup 或它的一个子类,没有滚动条出现。

The "problem" is that you are extending RelativeLayout, which is a subclass of ViewGroup. I found that I had no problem doing what you described in your question to get a custom View to display scrollbars, but when I extended ViewGroup or one of its subclasses, no scrollbars appeared.

我终于想起了的ViewGroup ,默认情况下,不画自己。我加了一行到我的自定义的ViewGroup 的构造函数: setWillNotDraw(假);

I finally remembered that ViewGroups, by default, don't draw themselves. I added one line to my custom ViewGroup constructor: setWillNotDraw(false);

你怎么知道,滚动条出现了!

What do you know, the scrollbars appeared!

希望有所帮助。