【问题标题】:Scrollview - How do you make a scroll view display 2 children at a time yet still relative to the screen size?Scrollview - 如何使滚动视图一次显示 2 个子项,但仍相对于屏幕大小?
【发布时间】:2016-01-26 07:53:38
【问题描述】:

我想知道是否有任何方法可以让 ScrollView 中的孩子一次只显示 2 个孩子。我尝试让每个孩子的布局高度 = 0dp 并设置权重,但它会一次显示所有孩子。 我已经尝试将孩子的身高设置为特定的数量,但它只适用于我的特定屏幕尺寸。

这是我的示例代码:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:clickable="false"
android:focusableInTouchMode="false"
android:focusable="false">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#ff0000"
        android:layout_weight=".5"></LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#7458ff"
        android:layout_weight=".5"></LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#56ffe3"
        android:layout_weight=".5"></LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#ffffff"
        android:layout_weight=".5"></LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#fffd69"
        android:layout_weight=".5"></LinearLayout>
</LinearLayout>

Here is what it looks like when I set the view height to 0dp and use weights

But I only want it to display 2 at a time

【问题讨论】:

  • 你想为第一个和第二个线性布局做这个吗?或者每次你进入这个布局时,滚动后你只需要在那个屏幕上有两个布局?@Potato

标签: java android scrollview children


【解决方案1】:

一种解决方案是以编程方式设置每个孩子的高度

view.getLayoutParams().height = getHalfScreenSize();

半屏尺寸是这样的:

  DisplayMetrics metrics = new DisplayMetrics();
  getWindowManager().getDefaultDisplay().getMetrics(metrics).heightPixels/2;

【讨论】:

    【解决方案2】:

    你试过 LayoutInflater 吗?你有一个例子herehere

    【讨论】:

    • 你的意思是把所有的孩子都添加到一个水平布局中吗?我试图弄清楚如何在垂直布局中显示这些水平布局。 x.x
    • 对不起,我没有得到您的代码示例,您尝试在水平线性布局中添加元素。使用 LayoutInflater 动态添加滚动视图元素。
    • 您必须设计一个模板布局,并将其作为滚动视图的一个元素。
    猜你喜欢
    • 2021-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多