简体   繁体   English

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

[英]Scrollview - How do you make a scroll view display 2 children at a time yet still relative to the screen size?

I'm wondering if there is any way to make the children inside a ScrollView only show 2 children at a time. 我想知道是否有任何方法可以使ScrollView中的子代一次只显示2个子代。 I have tried making the layout heights of each children = 0dp and setting the weight but it displays all the children at once. 我尝试过使每个子级的布局高度= 0dp并设置权重,但它会一次显示所有子级。 I have tried setting the children heights to a specific amount and it works but only for my certain screen size. 我尝试将孩子的身高设置为特定的数量,但它仅适用于我的特定屏幕尺寸。

Here is my sample code : 这是我的示例代码:

<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 这是将视图高度设置为0dp并使用权重时的样子

But I only want it to display 2 at a time 但我只希望它一次显示2

Have you tried LayoutInflater ? 您是否尝试过LayoutInflater? You have an example here or here 你在这里这里有一个例子

One sollution is to set height of each child programmaticaly like 一种解决方案是通过编程方式设置每个孩子的身高,例如

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

And the half screen size like this: 半屏尺寸是这样的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让 ScrollView 自动滚动到底部? - How do I make ScrollView auto scroll to the bottom? 如何在Android Studio的滚动视图下进行相对布局 - how to make a relative layout under Scroll view in android studio 如何在android中制作一个贴在屏幕底部的布局,直到scrollview滚动到某个位置时它也会滚动? - How to make a layout in android that sticks to the bottom of the screen until the scrollview is scrolled to a certain position it gets scroll too? Android Studio如何显示屏幕大小(简单方法)? - How can you display screen size in Android Studio(simple way)? 如何让ScrollView在真正的RelativeLayout下滚动 - how to make ScrollView scroll under the real RelativeLayout 对 android 应用程序进行编程:如何使其显示一次性设置屏幕 - For programming an android app: How do I make it display a one time setup screen Android - 如何使按钮不可见但仍然有效? - Android - How do you make a button invisible but still active? 如何使列表视图可点击 - How do you make a list view clickable ADT - 如何以图形方式编辑大于屏幕大小的滚动视图? - ADT - How do I graphicly edit a scrollview that is bigger than the screen size? 滚动视图中的ListView我的滚动视图移到列表视图的顶部。 我该如何预防? - ListView in Scroll View my scrollview moves to top of listview. How do I prevent this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM