Android ScrollView 设置显示内容的高度

标签 android xml layout scrollview xamarin

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

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

我现在拥有的是:

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

填充后, ScrollView 和线性布局会根据内容的需要进行拉伸(stretch)并显示所有内容,而不是具有 250dp/px 的“窗口”(任何测量值都是很好),其中的内容可以滚动。

我是 android 平台的新手,所以也许答案很明显,我只是不懂语言,但我们将不胜感激! 谢谢

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

最佳答案

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);
   }
}

然后剩下的一件小事是在您的值文件夹的 attrs.xml 文件中声明您的样式(如果您没有,只需在项目的 res 文件夹的值文件夹中创建一个具有此名称的 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很快就结束了!

关于Android ScrollView 设置显示内容的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17683789/

相关文章:

android - 如何在android中心画一条线和一些文本?

Android Studio 3.0 构建变体与风格不匹配

android - 如何以编程方式在 Lenovo 设备中为我的应用程序启用自动启动选项?

xml - XSLT:合并 xml 文件的简单方法

java - 无法为 GridView 布局设置背景颜色

xml - SOAP::Data::Builder,当没有提供值时删除 xsi:nil ="true"

java - Android MediaPlayer.OnCompletionListener() 的意外行为

css - &::before 伪元素未正确对齐 - CSS(3) 盒模型 - LESS

html - 如何让一个对象不占用它的自然宽度

visual-studio - 有没有布局比较/vssetting分享的地方?