如何在 Activity 中设置滚动视图的最大高度?

How can I set a maximum height for a Scroll View in an Activity?

我想达到的目标: 一个 java Activity 和属于 xml 的布局,用户可以在其中输入所有玩家的名字。这是它的样子:

如果您单击加号按钮,您可以输入另一个玩家(Spieler 是德语玩家的意思)。在达到最大高度之前,它应该是一个环绕视图。

我在这里发现了一个非常相似的问题: How to set a maximum height with wrap content in android?

有注释给出的代码:"you can add this to any view (override onMeasure in a class inherited from a view)"

我的问题是: 我属于 java-文件的 xml-layout 继承自 Activiy,因此不知道 super.onMeasure( ) 方法。你有什么建议吗?我可以将一个 Activity-java-文件和一个视图-java-文件用于一个布局吗?非常感谢!

您必须创建一个扩展 ScrollView 的自定义视图,然后在您的 xml

中使用该视图
        public class ScrollViewWithMaxHeight extends ScrollView {

            public static int WITHOUT_MAX_HEIGHT_VALUE = -1;

            private int maxHeight = WITHOUT_MAX_HEIGHT_VALUE;

            public ScrollViewWithMaxHeight(Context context) {
                super(context);

 init(context, null, 0, 0);
            }

            public ScrollViewWithMaxHeight(Context context, AttributeSet attrs) {
                super(context, attrs);
 init(context, attrs, 0, 0);
            }

            public ScrollViewWithMaxHeight(Context context, AttributeSet attrs, int defStyle) {
                super(context, attrs, defStyle);
 init(context, attrs, defStyle, 0);
            }
           private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes){
    TypedArray a = context.getTheme().obtainStyledAttributes(
                    attrs, R.styleable.custom_ScrollViewWithMaxHeight,  defStyleAttr, defStyleRes);
     maxHeight = 
     a.getDimensionPixelSize(R.styleable.max_height,maxHeight);
        }

            @Override
            protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
                try {
                    int heightSize = MeasureSpec.getSize(heightMeasureSpec);
                    if (maxHeight != WITHOUT_MAX_HEIGHT_VALUE
                            && heightSize > maxHeight) {
                        heightSize = maxHeight;
                    }
                    heightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.AT_MOST);
                    getLayoutParams().height = heightSize;
                } catch (Exception e) {
                    LogManager.error(this, "onMesure", "Error forcing height", e);
                } finally {
                    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
                }
            }

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

您还必须在您的值文件夹中创建一个 attrs.xml 文件并将其添加到其中。

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
    <declare-styleable name="custom_ScrollViewWithMaxHeight">
        <attr name="max_height" format="dimension" />
    </declare-styleable>
</resources>

然后您可以像这样引用视图

<the.package.where.the.view.is.ScrollViewWithMaxHeight
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 app:max_height="150dp">
</the.package.where.the.view.is.ScrollViewWithMaxHeight>

就这么简单。 而不是使用 ScrollView,我更喜欢你必须使用 RecycleViewAdapter 和加号图标使用 Fab 按钮。

无需创建自定义视图和所有内容,您可以使用 .xml 文件中的 android:layout_height="wrap_content" 按钮实现 RecycleView 的最大高度和底部的 Fab 按钮作为

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.tejadroid.testing.MainActivity"
    tools:showIn="@layout/app_bar_main">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_item"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clipToPadding="false"
            android:paddingBottom="56dp" />

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right|bottom"
            android:layout_margin="@dimen/fab_margin"
            app:srcCompat="@android:drawable/ic_input_add" />
    </FrameLayout>
</RelativeLayout>