我正在创建一个自定义类来设置NestedScrollView
的最大高度,基于此StackOverflow问题中提供的答案:
如何在Android中设置嵌套滚动视图的最大高度?
但是,当我在activity_main.xml
代码布局中包含自定义类 (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 ipsumnLorem ipsumnLorem ipsumnLorem ipsum
nLorem ipsumnLorem ipsumnLorem ipsumnLorem ipsumnLorem ipsumnLorem ipsum
nLorem ipsumnLorem ipsumnLorem 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 ipsumnLorem ipsumnLorem ipsumnLorem ipsum
nLorem ipsumnLorem ipsumnLorem ipsumnLorem ipsumnLorem ipsumnLorem ipsum
nLorem ipsumnLorem ipsumnLorem ipsumLorem ipsumnLorem ipsumnLorem ipsumnLorem ipsum
nLorem ipsumnLorem ipsumnLorem ipsumnLorem ipsumnLorem ipsumnLorem ipsum
nLorem ipsumnLorem ipsumnLorem ipsum" />
</com.example.testgradle.MaxHeightNestedScrollView>
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Lorem ipsumnLorem ipsumnLorem ipsumnLorem ipsum
nLorem ipsumnLorem ipsumnLorem ipsumnLorem ipsumnLorem ipsumnLorem ipsum
nLorem ipsumnLorem ipsumnLorem ipsum" />
</LinearLayout>
</ScrollView>
</LinearLayout>
您的MaxHeightNestedScrollView
不完整,因为它没有指定如何使用 xml 中的maxHeight
属性。在下面修改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 中显示滚动条,只需在 xml 中将android: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 ipsumnLorem ipsumnLorem ipsumnLorem ipsum
nLorem ipsumnLorem ipsumnLorem ipsumnLorem ipsumnLorem
ipsumnLorem ipsum
nLorem ipsumnLorem ipsumnLorem 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 ipsumnLorem ipsumnLorem ipsumnLorem ipsum
nLorem ipsumnLorem ipsumnLorem ipsumnLorem ipsumnLorem ipsumnLorem ipsum
nLorem ipsumnLorem ipsumnLorem ipsumLorem ipsumnLorem ipsumnLorem ipsumnLorem ipsum
nLorem ipsumnLorem ipsumnLorem ipsumnLorem ipsumnLorem ipsumnLorem ipsum
nLorem ipsumnLorem ipsumnLorem ipsum" />
</com.example.testgradle.MaxHeightNestedScrollView>
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Lorem ipsumnLorem ipsumnLorem ipsumnLorem ipsum
nLorem ipsumnLorem ipsumnLorem ipsumnLorem ipsumnLorem ipsumnLorem ipsum
nLorem ipsumnLorem ipsumnLorem 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">