【发布时间】:2018-02-14 22:36:44
【问题描述】:
我有这个奇怪的“错误”。在滚动视图中更改其背景颜色的按钮会导致滚动视图向上滚动。
布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="@layout/img_loading" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textIsSelectable="true"
android:id="@+id/txt_read_story_title" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textIsSelectable="true"
android:id="@+id/txt_read_story_author"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textIsSelectable="true"
android:id="@+id/txt_read_story_content"
/>
<include layout="@layout/inc_reactions_buttons" />
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
在 inc_reactions_buttons.xml 布局中,水平线性布局中有六个按钮。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal">
<ImageView
android:id="@+id/btn_funny"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_reaction_funny" />
...
</LinearLayout>
在此按钮的 OnClickListener 中,我更改了它们的启用和背景颜色
mBtnLove.setEnabled(false);
mBtnLove.setBackground(Color.parseColor("#FF000000"))
问题是当一个按钮被点击时,scrollView 滚动到顶部。 如果我删除 setBackground,则不会发生滚动。 我已经尝试过使用其他类型的视图(ImageView、ImageButton),但结果是一样的。 发生在 API 25 和 26 上(未在早期版本上测试)
有人知道原因吗?
谢谢
【问题讨论】:
-
能否也提供“inc_reactions_buttons”的布局?
-
我已经用部分 inc_reactions_buttons 布局更新了答案。无论如何,我也尝试直接在活动布局中(在底部)添加一个图像视图,并且行为是相同的,每当我调用 setBackgroundColor 时,滚动视图向上滚动:(
-
好的,我找到了一些东西,为了简化答案,我省略了 TextView 的一些属性,它们具有“textIsSelectable=true”。使用此属性,每当单击按钮时,滚动视图都会滚动到带有 textIsSelectable 的元素。
标签: android scrollview