简体   繁体   English

滚动视图内的线性布局权重

[英]linear layout weight inside scrollview

what I am trying to do is some thing like this. 我想做的是这样的事情。

在此处输入图片说明

image one 形象一

No matter which device is it. 无论是哪个设备。 It should display ony four rows on the screen and later user can scroll to see other rows 它应该在屏幕上仅显示四行,以后用户可以滚动查看其他行

I am using veritical linearlayout weight to achieve this,But it does not happening the way i am trying to achieve this. 我正在使用垂直线性布局权重来实现这一目标,但是它并没有以我试图实现这一目标的方式发生。 its give me screen like this when i add more rows 当我添加更多行时,它会给我这样的屏幕

在此处输入图片说明

image two 图片二

below is my layout xml which gives screen as image one but adding more rows gives image two view 下面是我的布局xml,它将屏幕显示为图像一,但添加更多行则显示了图像二

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/lightGray"
android:orientation="vertical">

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar"
        android:fillViewport="true">

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

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:padding="1dp"
                android:weightSum="4"

                >


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:orientation="horizontal"
                    android:weightSum="3">


                    <ImageView
                        android:id="@+id/abulation"
                        style="@style/icon"
                        android:background="@drawable/wudu_96" />

                    <ImageButton
                        android:id="@+id/masjid"
                        style="@style/icon"
                        android:background="@drawable/masjid_96" />


                    <ImageView
                        android:id="@+id/restroom"
                        style="@style/icon"
                        android:background="@drawable/restroom_96" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:orientation="horizontal"
                    android:weightSum="3">

                    <ImageView
                        android:id="@+id/dressing"
                        style="@style/icon"
                        android:background="@drawable/dressing_96" />

                    <ImageButton
                        style="@style/icon"
                        android:background="@drawable/icon_selector"
                        android:scaleType="fitXY" />


                    <ImageButton
                        android:id="@+id/Favourite"
                        style="@style/icon"
                        android:background="@drawable/favourite_96"
                        android:onClick="random"
                        android:scaleType="fitXY" />


                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:orientation="horizontal"
                    android:weightSum="3">

                    <ImageView
                        style="@style/icon"
                        android:background="@drawable/fear_96" />

                    <ImageView
                        style="@style/icon"
                        android:background="@drawable/fear_96" />

                    <ImageView
                        style="@style/icon"
                        android:background="@drawable/fear_96" />
                </LinearLayout>


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:orientation="horizontal"
                    android:weightSum="3">

                    <ImageView
                        style="@style/icon"
                        android:background="@drawable/fear_96" />

                    <ImageView
                        style="@style/icon"
                        android:background="@drawable/fear_96" />

                    <ImageView
                        style="@style/icon"
                        android:background="@drawable/fear_96" />
                </LinearLayout>

            </LinearLayout>



        </LinearLayout>
    </ScrollView>
  </android.support.v4.widget.NestedScrollView>
</LinearLayout>

I know i am doing wrong thing to achieve this . 我知道我在做这件事上做错了。 Please help me how can i get the view i want . 请帮助我如何获得想要的视图。

Try this. 尝试这个。 You have wrapped your view in one more parent LinearLayout in ScrollView that might just be the reason for your problem. 您已将视图包装在ScrollView中的另一个父级LinearLayout中,这可能只是问题的原因。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:orientation="vertical">

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/toolbar"
            android:fillViewport="true">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:weightSum="4">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:weightSum="3"
                    android:layout_height="0dp"
                    android:layout_weight="1">
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:weightSum="3"
                    android:layout_height="0dp"
                    android:layout_weight="1">
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:weightSum="3"
                    android:layout_height="0dp"
                    android:layout_weight="1">
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:weightSum="3"
                    android:layout_height="0dp"
                    android:layout_weight="1">
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                </LinearLayout>
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:weightSum="4">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:weightSum="3"
                    android:layout_height="0dp"
                    android:layout_weight="1">
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:weightSum="3"
                    android:layout_height="0dp"
                    android:layout_weight="1">
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:weightSum="3"
                    android:layout_height="0dp"
                    android:layout_weight="1">
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:weightSum="3"
                    android:layout_height="0dp"
                    android:layout_weight="1">
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                    <ImageView
                        android:src="@mipmap/ic_launcher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>
                </LinearLayout>
            </LinearLayout>
        </ScrollView>
    </android.support.v4.widget.NestedScrollView>
</LinearLayout>

Try this. 尝试这个。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/lightGray"
android:orientation="vertical">

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar"
        android:fillViewport="true">

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

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:padding="1dp"
                android:weightSum="4"

                >


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:orientation="horizontal"
                    android:weightSum="3">


                    <ImageView
                        android:id="@+id/abulation"
                        android:layout_width="wrap_content"
                        android:layout_weight="1"
                        android:layout_height="match_parent"
                        android:background="#FF0F0F" />

                    <ImageButton
                        android:id="@+id/masjid"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:layout_width="wrap_content"
                        android:background="#D0060F" />


                    <ImageView
                        android:id="@+id/restroom"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:layout_width="wrap_content"
                        android:background="#1F0F0F" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:orientation="horizontal"
                    android:weightSum="3">

                    <ImageView
                        android:id="@+id/dressing"
                        android:layout_width="wrap_content"
                        android:layout_weight="1"
                        android:layout_height="match_parent"
                        android:background="#FF0F0F" />

                    <ImageButton
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:layout_width="wrap_content"
                        android:background="#1F0F0F"
                        android:scaleType="fitXY" />


                    <ImageButton
                        android:id="@+id/Favourite"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:layout_width="wrap_content"
                        android:background="#D0060F"
                        android:onClick="random"
                        android:scaleType="fitXY" />


                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:orientation="horizontal"
                    android:weightSum="3">

                    <ImageView
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:layout_width="wrap_content"
                        android:background="#D0060F" />

                    <ImageView
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:layout_width="wrap_content"
                        android:background="#1F0F0F" />

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_weight="1"
                        android:layout_height="match_parent"
                        android:background="#FF0F0F" />
                </LinearLayout>


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:orientation="horizontal"
                    android:weightSum="3">

                    <ImageView
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:layout_width="wrap_content"
                        android:background="#1F0F0F" />

                    <ImageView
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:layout_width="wrap_content"
                        android:background="#D0060F" />

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_weight="1"
                        android:layout_height="match_parent"
                        android:background="#FF0F0F" />
                </LinearLayout>

            </LinearLayout>



        </LinearLayout>
    </ScrollView>
  </android.support.v4.widget.NestedScrollView>
</LinearLayout>

Hope it helps.! 希望能帮助到你。!

Here's what you need to do: Firstly we get rid of android:weightSum property from the main vertical LinearLayout and the android:layout_weight from each of the horizontal LinearLayout . 这是您需要做的:首先,我们摆脱主要垂直LinearLayoutandroid:weightSum属性和每个水平LinearLayoutandroid:layout_weight We also need to set id to our vertical LinearLayout as android:id=@+id/mainContainerLL . 我们还需要将id设置为android:id=@+id/mainContainerLL作为垂直LinearLayout This is the sample layout that i derived for testing from your layout: 这是我从您的布局中得出的用于测试的示例布局:

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar"
        android:fillViewport="true">

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

            <LinearLayout
                android:id="@+id/rowContainerLL"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:padding="1dp">


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/holo_red_light"
                    android:orientation="horizontal"
                    android:weightSum="3">

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/darker_gray"

                    android:orientation="horizontal"
                    android:weightSum="3">


                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/holo_red_light"

                    android:orientation="horizontal"
                    android:weightSum="3">
                </LinearLayout>


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/darker_gray"

                    android:orientation="horizontal"
                    android:weightSum="3">

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/holo_red_light"

                    android:orientation="horizontal"
                    android:weightSum="3">

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/darker_gray"

                    android:orientation="horizontal"
                    android:weightSum="3">

                </LinearLayout>

            </LinearLayout>


        </LinearLayout>
    </ScrollView>
</android.support.v4.widget.NestedScrollView>

Now, for the java coding part: We use a ViewTreeObserver to get height at runtime (observer gives us a callback when views are about to be drawn and their heights are calculated so, we need that). 现在,对于Java编码部分:我们使用ViewTreeObserver在运行时获取高度(观察者ViewTreeObserver我们提供回调,以便在将要绘制视图并计算其高度时,我们需ViewTreeObserver )。 There, we set height to each of our horizontal LinearLayout : 在那里,我们为每个水平LinearLayout设置高度:

final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.rowContainerLL);
    ViewTreeObserver viewTreeObserver = linearLayout.getViewTreeObserver();

    viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            linearLayout.getViewTreeObserver().removeOnPreDrawListener(this);

            View child;
            LinearLayout.LayoutParams params;
            final int viewHeight = linearLayout.getHeight() / 4;

            for (int i = 0; i < linearLayout.getChildCount(); i++) {
                child = linearLayout.getChildAt(i);

                params = (LinearLayout.LayoutParams) child.getLayoutParams();
                params.height = viewHeight;

                child.setLayoutParams(params);
            }

            return false;
        }
    });

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM