1

我想要的图片: 在此处输入图像描述

关于文字:我想禁用带有格式文本的滚动HorizontalScrollView

XML的一部分:

<ScrollView
    android:id="@+id/review_scrollview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollbars="horizontal"
    android:fillViewport="true"
    android:layout_marginTop="10dp">

    <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">


        <TextView
            android:id="@+id/tt_review_content"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Some long text"
            android:scrollHorizontally="true"/>

    </HorizontalScrollView>

</ScrollView>

方法(无效):

private void setTtAreaWrapContent(boolean value) {
    ViewGroup.LayoutParams params = textField.getLayoutParams();
    if(value) { // Wrap content
        textField.setWidth(scrollView.getWidth());
    } else { // Scroll
        params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
    }
    textField.setLayoutParams(params);
}
4

2 回答 2

0

有一种非常简单的方法可以做到这一点。

检查用户是否要滚动,然后将其存储在布尔变量中shouldScroll

现在在 中执行此操作onCreate

HorziontalScrollView scrollView= (HorizontalScrollView)findViewById(R.id.scrollView);

if(!shouldScroll)
scrollView.setOnTouchListener(new OnTouch());

您还需要定义一个类OnTouch扩展OnTouchListener

private class OnTouch implements OnTouchListener
{
    @Override
    public boolean onTouch(View v, MotionEvent event) {
    return true;
    }
}

现在对于 TextView 格式,只需使用android:singleLine="false"或使用android:width="0dip"

于 2015-07-23T13:11:30.983 回答
-2

我对代码的需求完全一样,所以我最初提出了一个具有相同布局的 ViewSwitcher,但一个带有 Horizo​​ntalScrollView,一个没有它。并根据设置切换到相应的视图。

但是,我找到了一个更简单的解决方案。重新创建 Activity 后(当用户更改包装设置时),使用 onCreate 方法中的代码:

  if (isWrapContent)
    {
        View mTV = findViewById(R.id.my_text_view);
        HorizontalScrollView myHSV = (HorizontalScrollView) findViewById(R.id.my_hsv);
        ViewGroup parent =  (ViewGroup) myHSV.getParent();
        ViewGroup.LayoutParams params = myHSV.getLayoutParams();
        int index = parent.indexOfChild(myHSV);
        myHSV.removeAllViews();
        parent.removeView(myHSV);
        parent.addView(mTV, index, params);
    }

您需要相应地使用您的 id 和变量。

它将删除带有嵌入文本视图的水平滚动视图,然后重新插入文本视图。

我的回答提供了关于如何解决所问问题的直接有效的答案,而不是冻结水平滚动视图或想知道问题的重点,就像在其他评论和答案中所做的那样。

于 2017-01-02T00:13:26.803 回答