Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Scrollview-如何使滚动视图一次显示2个子项,但仍然相对于屏幕大小?_Java_Android_Scrollview_Children - Fatal编程技术网

Java Scrollview-如何使滚动视图一次显示2个子项,但仍然相对于屏幕大小?

Java Scrollview-如何使滚动视图一次显示2个子项,但仍然相对于屏幕大小?,java,android,scrollview,children,Java,Android,Scrollview,Children,我想知道是否有办法让滚动视图中的孩子一次只显示两个孩子。我尝试过将每个孩子的布局高度设为0dp并设置权重,但它会同时显示所有孩子。 我试着将孩子们的身高设置为一个特定的数值,但它只适用于我特定的屏幕尺寸 以下是我的示例代码: <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="matc

我想知道是否有办法让滚动视图中的孩子一次只显示两个孩子。我尝试过将每个孩子的布局高度设为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>


你试过放平一点吗?您有一个例子,一个解决方案是设置每个子程序的高度

view.getLayoutParams().height = getHalfScreenSize();
半屏幕大小如下:

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

您的意思是将所有子项添加到一个水平布局中吗?我试图弄清楚如何在垂直布局中显示这些水平布局。x、 xSorry,我没有得到您的代码示例,您试图在水平线性布局中添加元素。使用LayoutInflater动态添加scrollview元素。您必须设计一个模板布局,并将其作为滚动视图的一个元素进行膨胀。是否要将其用于第一个和第二个线性布局?或者每次你进入这个布局时,滚动后只需要在屏幕上显示两个布局?@Potato