[英]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并使用权重时的样子
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.