【问题标题】:Scroll-up button Android向上滚动按钮 Android
【发布时间】:2020-03-11 14:52:07
【问题描述】:

我有一个小问题。我在带有TextViews的布局scrollView中,现在我必须设置按钮以向上滚动scrollView,但我不知道如何在我的布局中设置好imageButton。这是我的架构。

Schema

我必须将这个 imageButton 设置到位,我在模式中显示,TextView5 非常长,当它开始滚动时,imageButton 应该开始可见,在滚动之前它应该是不可见的。

要向上滚动,我正在使用代码:

buttonScrollTop.setOnClickListener {
            scrollView.fullScroll(ScrollView.FOCUS_UP)
        }

我的xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:background="@color/colorBackgroundWhite">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/scrollView"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp">

            <TextView
                android:id="@+id/TextView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="30sp"
                android:ellipsize="end"
                android:gravity="center"
                android:text="@string/text1" />

            <TextView
                android:id="@+id/TextView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:ellipsize="end"
                android:gravity="center"
                android:paddingBottom="8dp"
                android:text="@string/text2" />

            <TextView
                android:id="@+id/TextView3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/text3"/>

            <TextView
                android:id="@+id/TextView4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:gravity="center"
                android:justificationMode="inter_word"
                android:paddingBottom="4dp"
                android:paddingTop="4dp"
                android:text="@string/text4" />

            <TextView
                android:id="@+id/TextView5"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:justificationMode="inter_word"
                android:ellipsize="end"
                android:text="@string/bigstringtext5" />

            <ImageButton
                android:id="@+id/buttonScrollTop"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:srcCompat="@android:drawable/ic_menu_upload" />

            <TextView
                android:id="@+id/TextView6"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:justificationMode="inter_word"
                android:ellipsize="end"
                android:gravity="center"
                android:text="@string/text6" />
        </LinearLayout>
    </ScrollView>
</RelativeLayout>

但现在 imageButton 在 TextView5 下;/ 我不知道如何在模式中设置它。 感谢您的帮助:)

【问题讨论】:

  • ScrollView 之外的父布局 (RelativeLayout) 中使用 ImageButtonalignParentBottom="true"alignParentEnd="true",然后给您的 ImageButton 边距以使其完美。
  • 它可以工作,但不要让它在开始时不可见,你知道如何让它在开始时不可见并在开始滚动时可见吗? :)

标签: android xml button kotlin


【解决方案1】:

检查以下代码 - 它将在右侧角落显示您在架构中定义的图像按钮。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:background="@color/colorBackgroundWhite">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/scrollView"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp">

        <TextView
            android:id="@+id/TextView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="30sp"
            android:ellipsize="end"
            android:gravity="center"
            android:text="@string/text1" />

        <TextView
            android:id="@+id/TextView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:ellipsize="end"
            android:gravity="center"
            android:paddingBottom="8dp"
            android:text="@string/text2" />

        <TextView
            android:id="@+id/TextView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/text3"/>

        <TextView
            android:id="@+id/TextView4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:gravity="center"
            android:justificationMode="inter_word"
            android:paddingBottom="4dp"
            android:paddingTop="4dp"
            android:text="@string/text4" />

        <TextView
            android:id="@+id/TextView5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:justificationMode="inter_word"
            android:ellipsize="end"
            android:text="@string/bigstringtext5" />

        <TextView
            android:id="@+id/TextView6"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:justificationMode="inter_word"
            android:ellipsize="end"
            android:gravity="center"
            android:text="@string/text6" />
    </LinearLayout>
</ScrollView>

<ImageButton
    android:contentDescription="@null"
    android:id="@+id/buttonScrollTop"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentBottom="true"
    android:layout_margin="16dp"
    app:srcCompat="@android:drawable/ic_menu_upload"
    android:layout_alignParentRight="true" />

让按钮在滚动视图到达底部时可见

scrollView.getViewTreeObserver()
   .addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
        @Override
        public void onScrollChanged() {
            if (scrollView.getChildAt(0).getBottom()
                 <= (scrollView.getHeight() + scrollView.getScrollY())) {
                //scroll view is at bottom
        //set the button visibility to visible here
            } else {
                //scroll view is not at bottom
        //set the button visibility to gone here
            }
        }
    });

【讨论】:

  • 是的,这就是我需要的!谢谢 !但是 imageButton 一直是可见的,我想让它在开始时不可见,并且在开始滚动时让它可见:) 你知道怎么做吗?
  • 立即查看答案!如果您的问题得到解决,请点赞!谢谢!
  • 我正在尝试从 java 更改为 Kotlin,但它不起作用;/
  • 将上面的java代码复制粘贴到你的kotlin文件中,然后android studio本身会显示一个对话框,要求转换成kotlin。
  • 好吧,我成功了!谢谢:)
【解决方案2】:

我现在有一个错误。我在片段 1 中有函数,但这给片段 3 带来了问题。我在模式中显示了问题的所有结构。

schema

当我有活动时

viewpager.offscreenPageLimit = 1

当我打开也有 scrollView 的 Fragment 3 时,我得到了这个错误:

java.lang.IllegalStateException: scrollView1 must not be null

当我这样改变时

viewpager.offscreenPageLimit = 2(3,4,5 + etc) 

当我打开 Fragment 3 时,如果有大字符串我会延迟 1-2 秒,稍后我可以滚动。如果字符串很小,我没有这个问题。 有任何想法如何解决它?

【讨论】:

  • 尝试使用 show more 加载字符串...并发布您尝试过的代码。
猜你喜欢
  • 2017-04-04
  • 2015-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-24
  • 1970-01-01
  • 1970-01-01
  • 2017-07-27
相关资源
最近更新 更多