【问题标题】:Android: Why text inside TextView scrolls in LinearLayout but not in RecyclerviewAndroid:为什么 TextView 中的文本在 LinearLayout 中滚动但在 Recyclerview 中不滚动
【发布时间】:2017-12-20 06:49:17
【问题描述】:

RecyclerView(屏幕A)和FrameLayout(屏幕B)中有TextView,但两者都有maxLines=10

在 RecyclerView 中,textview 仅呈现 10 行,其他文本被忽略。

但LinearLayout中的textview,显示10行但允许用户滚动。

如何禁用滚动并仅显示使用 maxLines 设置的最大行数。

我已经试过了,

  • android:isScrollContainer="false" - 不起作用

  • android:enabled="false" - 它禁用文本视图中的超链接点击

更新:

我不希望 RecyclerView 中的 textview 滚动,我只想禁用 LinearLayout 中的滚动。

【问题讨论】:

  • 在此处添加您要禁用滚动的 xml。

标签: android android-layout android-linearlayout textview


【解决方案1】:

试过下面这行吗?

recyclerView.setNestedScrollingEnabled(false);

【讨论】:

    【解决方案2】:

    像这样创建NoScrollTextView 扩展TextView

    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 defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        public NoScrollTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
        }
    
        @Override
        public void scrollTo(int x, int y) {
            //do nothing
        }
    }
    

    然后在 xml 中使用:

    <com.example.NoScrollTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLines="10"/>
    

    【讨论】:

    • 是的,我也试过这个,只是在寻找其他更好的方法,只需修改或设置视图属性,而不是创建自定义视图。
    • 请不要复制粘贴相同的答案在这里stackoverflow.com/questions/24027108/…
    • 我已经修改过了。
    【解决方案3】:

    刚刚在课堂上设置

     TextView tv = (TextView) view.findViewById(R.id.tv);
      tv.setMovementMethod(null);
    

    在布局中

                  <TextView
                    android:id="@+id/tv"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="3dp"
                    android:maxLines="8"
                    android:text="Lorem Ipsum is simply dummy text of the dummy text of t printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy."
                    android:textSize="14sp" />
    

    【讨论】:

      猜你喜欢
      • 2020-11-01
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-26
      • 1970-01-01
      • 2020-11-14
      相关资源
      最近更新 更多