Android:用于设置 NestedScrollView 的最大高度的自定义 class 不起作用(没有滚动条)
Android: Custom class for setting the max height of a NestedScrollView does not work (there is no scrollbar)
我正在创建自定义 class 以设置 NestedScrollView
的最大高度,基于此 Whosebug 问题中提供的答案:
How to set the Maximum Height for a NestedScrollView in Android?
但是,当我在 activity_main.xml
代码布局中包含自定义 class (MaxHeightNestedScrollView
) 时,没有出现滚动条 MaxHeightNestedScrollView
中的 TextView 超出了定义的最大高度。下面是 MaxNestedScrollView
:
的代码
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);
}
}
下面是 values
文件夹中 attrs.xml
文件的代码:
<?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>
您将高度换行到自定义滚动视图的 wrap_content
<com.example.testgradle.MaxHeightNestedScrollView
android:layout_width="match_parent"
android:layout_height="provide your desired height"
android:scrollbars="vertical"
android:fadeScrollbars="false">
您的 MaxHeightNestedScrollView
不完整,因为它没有指定如何使用 xml 中的 maxHeight
属性。使用下面修改的 MaxHeightNestedScrollView
class(差异被注释掉)。
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 中显示滚动条,只需将 android:scrollbars="vertical"
属性添加到 xml 中的 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>
希望对您有所帮助。
我正在创建自定义 class 以设置 NestedScrollView
的最大高度,基于此 Whosebug 问题中提供的答案:
How to set the Maximum Height for a NestedScrollView in Android?
但是,当我在 activity_main.xml
代码布局中包含自定义 class (MaxHeightNestedScrollView
) 时,没有出现滚动条 MaxHeightNestedScrollView
中的 TextView 超出了定义的最大高度。下面是 MaxNestedScrollView
:
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);
}
}
下面是 values
文件夹中 attrs.xml
文件的代码:
<?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>
您将高度换行到自定义滚动视图的 wrap_content
<com.example.testgradle.MaxHeightNestedScrollView
android:layout_width="match_parent"
android:layout_height="provide your desired height"
android:scrollbars="vertical"
android:fadeScrollbars="false">
您的 MaxHeightNestedScrollView
不完整,因为它没有指定如何使用 xml 中的 maxHeight
属性。使用下面修改的 MaxHeightNestedScrollView
class(差异被注释掉)。
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 中显示滚动条,只需将 android:scrollbars="vertical"
属性添加到 xml 中的 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>
希望对您有所帮助。