【问题标题】:can not scroll the ViewPager when touching TextView(with android:gravity="center") in it触摸 TextView(带有 android:gravity="center")时无法滚动 ViewPager
【发布时间】:2013-05-30 16:25:12
【问题描述】:

在我的 ViewPager 中,LinearLayout 保存了一个 ImageButton 和 TextView,现在我将它们更改为一个带有 coupound drawable 的 TextView。

<?xml version="1.0" encoding="utf-8"?>
<com.iclinux.android.custom_views.NoScrollTextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:iclinux="http://schemas.android.com/apk/res-auto"
    android:clipChildren="false"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:gravity="center"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"
    android:drawableTop="@drawable/icon_bg"
    style="@style/EllipsizedSingleLineTextView"
    android:textSize="@dimen/grid_item_title_size"
    >
</com.iclinux.android.custom_views.NoScrollTextView>

我的问题是:触摸 TextView 时我无法向左或向右翻转,但如果删除“android:gravity="center",它可以工作......不幸的是,文本无论如何都没有居中......

public class NoScrollTextView extends TextView {

    public NoScrollTextView(Context context) {
        super(context);
    }

    public NoScrollTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public NoScrollTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_MOVE)
            return false;
        return super.onTouchEvent(event);
    }
}

这里发生了什么?非常感谢。

【问题讨论】:

  • 我遇到的一个有趣的错误。谢谢你。我投了你一票。
  • 同样的问题,找到答案here

标签: android textview


【解决方案1】:

android:singleLine="true" 强制 android:scrollHorizontally 设置为 true,并且根据我的测试,只有当您更改 android:gravity 时(即默认重力似乎没有给出这个问题)。

我在 TextView 中解决了这个问题,只需将 android:singleLine="true" 替换为 android:maxLines="1"

在此更改之后,我对 ViewPager 不再有任何问题。现在它在使用 android:gravity="right" 触摸元素时按预期滚动。

【讨论】:

  • 天啊!我在这样一个愚蠢的问题上浪费了两个小时,最后我找到了解决它的答案!我不敢相信谷歌把这些糟糕的错误放进去。
  • 天啊!简而言之,这就是 Android。
【解决方案2】:

通过覆盖解决:

@TargetApi(14)
@Override
public boolean canScrollHorizontally(int direction) {
    return false;
}

【讨论】:

  • 只有上帝知道为什么不赞成。这是唯一对我有用的东西。
【解决方案3】:

我做到了:

textView.setHorizontallyScrolling(false);

【讨论】:

    【解决方案4】:

    我遇到过这种情况。我的解决方案;

    我在我的 XML 视图中添加了android:scrollHorizontally="false",然后我使用了android:maxLines 而不是android:singleLine 属性。现在可以正常使用了。

    来自 attrs.xml 的文档;

    @deprecated 该属性已被弃用。使用maxLines 而是更改静态文本的布局,并使用 textMultiLine 标志在 inputType 属性中 对于可编辑的文本视图(如果 singleLine 和 inputType 都是 提供时,inputType 标志将覆盖 singleLine 的值)。

    &lt;attr name="singleLine" format="boolean" /&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-29
      • 1970-01-01
      相关资源
      最近更新 更多