首页 > 解决方案 > Android:用于设置 NestedScrollView 最大高度的自定义类不起作用(没有滚动条)

问题描述

我正在创建一个自定义类来设置 a 的最大高度NestedScrollView,基于此 StackOverflow 问题中提供的答案:

如何在 Android 中设置 NestedScrollView 的最大高度?

但是,当我在代码布局中包含自定义类 ( MaxHeightNestedScrollView)时,当 TextView 内的超出定义的最大高度时,不会出现滚动条。下面是代码:activity_main.xmlMaxHeightNestedScrollViewMaxNestedScrollView

import android.content.Context;
import android.util.AttributeSet;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;

public class MaxHeightNestedScrollView extends NestedScrollView {

    private int maxHeight = -1;

    public MaxHeightNestedScrollView(@NonNull Context context) {
        super(context);
    }

    public MaxHeightNestedScrollView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MaxHeightNestedScrollView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public int getMaxHeight() {
        return maxHeight;
    }

    public void setMaxHeight(int maxHeight) {
        this.maxHeight = maxHeight;
    }

    public void setMaxHeightDensity(int dps){
        this.maxHeight = (int)(dps * getContext().getResources().getDisplayMetrics().density);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (maxHeight > 0) {
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

以下是attrs.xml文件values夹中文件的代码:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MaxHeightNestedScrollView">
        <attr name="maxHeight" format="dimension" />
    </declare-styleable>
</resources>

下面是代码activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        android:layout_weight="1"
        android:fillViewport="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                android:id="@+id/textView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Lorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum
\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum
\nLorem ipsum\nLorem ipsum\nLorem ipsum" />

            <com.example.testgradle.MaxHeightNestedScrollView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                app:maxHeight="130dp">
                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textSize="20dp"
                    android:textColor="#000000"
                    android:text="Lorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum
\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum
\nLorem ipsum\nLorem ipsum\nLorem ipsumLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum
\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum
\nLorem ipsum\nLorem ipsum\nLorem ipsum" />
            </com.example.testgradle.MaxHeightNestedScrollView>

            <TextView
                android:id="@+id/textView3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Lorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum
\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum
\nLorem ipsum\nLorem ipsum\nLorem ipsum" />
        </LinearLayout>
    </ScrollView>
</LinearLayout>

标签: javaandroidscrollviewandroid-nestedscrollview

解决方案


MaxHeightNestedScrollView的不完整,因为它没有指定如何使用maxHeightxml 中的属性。使用下面的修改MaxHeightNestedScrollView类(差异被注释掉)。

MaxHeightNestedScrollView.java

public class MaxHeightNestedScrollView extends NestedScrollView {

private int maxHeight = -1;

public MaxHeightNestedScrollView(@NonNull Context context) {
    this(context, null, 0); // Modified changes
}

public MaxHeightNestedScrollView(@NonNull Context context, @Nullable AttributeSet attrs) {
    this(context, attrs, 0); // Modified changes
}

public MaxHeightNestedScrollView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context, attrs, defStyleAttr); // Modified changes
}

 // Modified changes
private void init(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr){
    final TypedArray a = context.obtainStyledAttributes(
            attrs, R.styleable.MaxHeightNestedScrollView, defStyleAttr, 0);
    maxHeight = 
    a.getDimensionPixelSize(R.styleable.MaxHeightNestedScrollView_maxHeight, 0);
    a.recycle();
}

public int getMaxHeight() {
    return maxHeight;
}

public void setMaxHeight(int maxHeight) {
    this.maxHeight = maxHeight;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    if (maxHeight > 0) {
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST);
    }
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}


此外,要在 NestingScrollView 中显示滚动条,只需在 xmlandroid:scrollbars="vertical"中的视图中添加属性。 更改后您的布局文件将如下所示。MaxHeightNestedScrollView

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:layout_weight="1"
    android:fillViewport="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Lorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum
            \nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem 
            ipsum\nLorem ipsum
            \nLorem ipsum\nLorem ipsum\nLorem ipsum" />

        <com.example.testgradle.MaxHeightNestedScrollView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical" // Modified changes
            app:maxHeight="130dp">
            <TextView
                android:id="@+id/textView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="20dp"
                android:textColor="#000000"
                android:text="Lorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum
                \nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum
                \nLorem ipsum\nLorem ipsum\nLorem ipsumLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum
                \nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum
                \nLorem ipsum\nLorem ipsum\nLorem ipsum" />
        </com.example.testgradle.MaxHeightNestedScrollView>

        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Lorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum
            \nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum\nLorem ipsum
            \nLorem ipsum\nLorem ipsum\nLorem ipsum" />
    </LinearLayout>
</ScrollView>
</LinearLayout>

希望这有帮助。


推荐阅读