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

3
我想实现的功能是:一个带有xml布局的java活动,用户可以输入所有玩家的名称。以下是它的外观:preview。如果单击加号按钮,您可以输入另一个玩家(“Spieler”是德语中的玩家)。它应该是一个包装视图,直到达到最大高度为止。
我在这里找到了一个非常相似的问题:如何在Android中使用wrap content设置最大高度?
给出了代码和评论:“您可以将此添加到任何视图中(覆盖从视图继承的类中的onMeasure)”
我的问题是:我的java文件与xml布局相关联,继承自Activiy,因此不知道super.onMeasure()方法。您有什么提示可以提供吗?我可以将Activity-java文件和View-java文件用于一个布局吗?非常感谢!
2个回答

2
你需要创建一个扩展了 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>

但是我在哪里可以定义maxHeight?我需要添加自定义属性吗? - Nina Seil
你需要实现你所创建的视图,而不是ScrollView。请查看我的编辑答案。 - Niza Siwale
setMaxHeight()方法在哪里使用?我已经实现了所有的代码,但它看起来和之前完全一样(包括ScrollView),只是我无法滚动(当单击加号时我不能访问新播放器)。 - Nina Seil
您可以在xml中调用它 app:max_height="yourMaxHeightInDp",也可以通过编程方式调用。 - Niza Siwale
使用新代码后,我遇到了两个错误:1)init(context, 0, 0, 0); --> 错误:(19, 23)错误:不兼容的类型:int无法转换为AttributeSet;2)a.getDimensionPixelSize(R.styleable.max_height,maxHeight); --> 错误:(35, 52)错误:找不到符号变量max_height。 - Nina Seil
显示剩余9条评论

0

这很简单。 我建议您使用RecycleViewAdapterFab按钮,而不是使用ScrollView

在不创建自定义视图的情况下,通过在您的.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>

我想要实现的是当你点击“加号”后,它会出现在最后一个字段的正下方,然后向下移动,一直到底部,然后开始滚动。我觉得这个建议可能行不通 :/ - Nina Seil
@NinaSeil,请参考我编辑过的答案,我认为现在你可以通过使用这个答案来实现你的要求。 - TejaDroid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接