【问题标题】:Android ScrollView set height for displayed contentAndroid ScrollView 设置显示内容的高度
【发布时间】:2013-07-15 01:28:52
【问题描述】:

我在尝试解决布局中具有静态高度可滚动区域的问题时遇到了很多麻烦。我有三个需要在同一个屏幕上显示的长列表,按顺序显示所有条目是完全不切实际的,因为如果你想跳过一个类别,你需要滚动过去可能有数百个条目。

假设我有一个内部带有线性布局的滚动视图,我希望它在屏幕上占据最大高度为 250dp,并且一旦填充的条目超过 250dp 的空间,就可以独立滚动。

p>

我现在拥有的是:

<ScrollView
    android:minWidth="25px"
    android:minHeight="150px"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/XXXXXXX"
    android:scrollbars="vertical">

    <LinearLayout
        android:orientation="vertical"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/XXXXXX" />
</ScrollView>

填充后,滚动视图和线性布局只会拉伸,只要它们需要适合内容并显示所有内容而不是具有 250dp/px 的“窗口”(任何测量都可以不错),其中的内容可滚动。

我是 android 平台的新手,所以也许答案是显而易见的,我只是不懂语言,但任何帮助将不胜感激! 谢谢

--- 已解决: 在具有高度的 ScrollView 之外放置一个 linearLayout。

【问题讨论】:

    标签: android xml layout scrollview xamarin


    【解决方案1】:

    this 回答的帮助下,我设法包装了一个非常基本的 ScrollView 组件,您可以在这种情况下使用:

    创建一个扩展 ScrollView 的自定义类并进行以下修改:

    public class MaxHeightScrollView extends ScrollView {
    
        private int maxHeight;
    
        public MaxHeightScrollView(Context context) {
            super(context);
        }
    
        public MaxHeightScrollView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init(context, attrs);
        }
    
        public MaxHeightScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init(context, attrs);
        }
    
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        public MaxHeightScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
            init(context, attrs);
        }
    
        private void init(Context context, AttributeSet attrs) {
            if (attrs != null) {
                TypedArray styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.MaxHeightScrollView);
                maxHeight = styledAttrs.getDimensionPixelSize(R.styleable.MaxHeightScrollView_maxHeight, 200); //200 is a default value
                styledAttrs.recycle();
            }
        }
    
       @Override
       protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
           heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST);
           super.onMeasure(widthMeasureSpec, heightMeasureSpec);
       }
    }
    

    然后剩下的一件小事是在你的 values 文件夹的 attrs.xml 文件中声明你的样式(如果你没有,只需在项目的 res 文件夹的 values 文件夹中创建一个具有此名称的 xml 文件)。在此处添加以下行:

    <declare-styleable name="MaxHeightScrollView">
        <attr name="maxHeight" format="dimension" />
    </declare-styleable>
    

    并按如下方式使用您的新 ScrollView:

    <com.yourpackage.MaxHeightScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:maxHeight="300dp">
    </com.yourpackage.MaxHeightScrollView>
    

    感谢 whizzle 快速结束这篇文章!

    【讨论】:

    • 它可以工作,只是我不知道如何在我的 xml 中使用该属性。
    【解决方案2】:

    我的简单解决方案。
    在 xml 中设置ScrollView 高度:

    android:layout_height="wrap_content"
    

    如果当前测量的高度> 200,则添加此代码以将最大高度设置为200

    sv.measure(0, 0);
    if (nsv.getMeasuredHeight() > 200) {
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                                          ViewGroup.LayoutParams.MATCH_PARENT, 200);
            sv.setLayoutParams(lp);
    }
    

    在我的例子中ScrollViewLinearLayout,所以我使用了LinearLayout.LayoutParams

    【讨论】:

    • 非常感谢,这正是我要找的。​​span>
    【解决方案3】:

    您需要的是maxHeight。但是由于视图不支持它,您必须使用一种解决方法。

    看看这个答案:

    https://stackoverflow.com/a/13811461/770467

    【讨论】:

    • 我得到了尺寸部分来处理你的文章,但现在那个区域没有滚动。我已经用新代码更新了这个问题。到目前为止,谢谢!
    • 这很奇怪,我刚刚运行了一个示例,它滚动正常,这是布局:pastebin.com/raw.php?i=R45Z7wBi
    • @ExceptionAl pasteBin 网站显示“此粘贴已被删除!”
    【解决方案4】:

    在 ScrollView 周围添加一个固定高度的布局

    【讨论】:

      猜你喜欢
      • 2013-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-31
      相关资源
      最近更新 更多