【问题标题】:How can I set a maximum height for a Scroll View in an Activity?如何在 Activity 中设置滚动视图的最大高度?
【发布时间】:2018-07-22 11:18:54
【问题描述】:

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

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

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

注释中给出了代码:“您可以将其添加到任何视图(在从视图继承的类中覆盖 onMeasure)”

这是我的问题: 我属于 xml-layout 的 java 文件继承自 Activiy,因此不知道 super.onMeasure() 方法。你有什么我能做的提示吗?我可以将 Activity-java-file 和 View-java-file 放在一个布局中吗?非常感谢!

【问题讨论】:

    标签: java android xml android-activity android-view


    【解决方案1】:

    您必须创建一个扩展 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;
                }
            }
    

    您还必须在 values 文件夹中创建一个 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>
    

    【讨论】:

    • 嗨!感谢您的回答!我定义了这个观点。现在我想在我的 xml 文件中声明它。我试过这个: schemas.android.com/apk/res/android" xmlns:custom="schemas.android.com/apk/res/com.example.ScrollViewWithMaxHeight" android:layout_width="match_parent" android:layout_height="wrap_content" android:fillViewport="true" custom:maxHeight=" 150">
    • 但是我在哪里可以定义 maxHeight?我必须添加自定义属性吗?
    • 您必须实现您创建的视图,而不是 ScrollView。看看我编辑的答案
    • 我用max_height 属性再次编辑了我的答案
    • 这是我所做的:1. 使用您的代码创建自定义视图,2. 将我的
    【解决方案2】:

    就这么简单。 而不是使用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>
    

    【讨论】:

    • 我想要实现的是加号按钮就在最后一个字段的正下方,如果你点击“加号”,按钮会向下,向下,直到它在底部然后滚动开始。我认为这个建议是不可能的:/
    • @NinaSeil,请参考我对您问题的编辑答案,我认为现在您可以通过使用此答案来满足您的要求。
    猜你喜欢
    • 1970-01-01
    • 2012-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-10
    相关资源
    最近更新 更多