13

在我的 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);
    }
}

这里发生了什么?多谢。

4

4 回答 4

21

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

我通过简单地替换为 TextView 解决了这个android:singleLine="true"问题android:maxLines="1"

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

于 2013-10-12T09:01:57.780 回答
10

通过覆盖解决:

@TargetApi(14)
@Override
public boolean canScrollHorizontally(int direction) {
    return false;
}
于 2013-06-05T06:48:33.823 回答
2

我通过以下方式完成了它:

textView.setHorizontallyScrolling(false);
于 2016-01-16T02:26:55.183 回答
0

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

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

attrs.xml 中的文档;

@deprecated 此属性已弃用。maxLines 改为 使用更改静态文本的布局,并将textMultiLineinputType 属性中的标志用于可编辑的文本视图(如果同时提供了 singleLine 和 inputType,则 inputType 标志将覆盖 singleLine 的值)。

<attr name="singleLine" format="boolean" />

于 2020-05-05T05:55:41.800 回答