java - 如何在 Activity 中设置 ScrollView 的最大高度?

标签 java android xml android-activity android-view

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

preview

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

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

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

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

最佳答案

您必须创建一个扩展 ScrollView 的自定义 View ,然后在您的 xml 中使用该 View

        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>

然后您可以像这样引用 View

<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>

关于java - 如何在 Activity 中设置 ScrollView 的最大高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48743907/

相关文章:

android - 为 SupportMapFragment 膨胀类 fragment 时出错

java - 当 namespace 前缀更改时,如何验证 XML 签名?

xml - 我对 DTD 的理解正确吗?

java - Webview 顶部的元素

java - 如何在 java 对象上使用 +-*/这样的操作

java - 使用spring boot和spring-boot-maven-plugin生成war时排除application.properties

java - 如何从函数返回 JButton 并使用它在 java 中的不同类中执行操作?

android - 我可以以编程方式打开/关闭 Android 开发模式吗?

java - 请告知我无法创建名称为 "con"的组的原因

android - 如何在 ListView 上实现长点击监听器