首页 > 解决方案 > Android:重新计算布局组件的尺寸

问题描述

我有一个带有少量组件的线性布局,其中大多数都设置了特定的尺寸,但 1 设置为0dpother 以填充剩余空间。

问题是在扩展 xml 布局之后,我以编程方式添加了一个组件,并且0dp视图不会缩小以适应这个新组件,因此它会使布局出现错误。

所有组件都有足够的空间,例如,如果我尝试在 xml 上添加“动态”组件(仅用于测试)并对其进行膨胀,一切都会很好。

如何强制 aLinearLayout重新计算视图高度?

我得到了什么: 代码如下:在此处输入图像描述

<?xml version="1.0" encoding="utf-8"?>

    <LinearLayout
        android:id="@+id/mainView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:orientation="vertical"
        android:paddingLeft="@dimen/smallMargin"
        android:paddingRight="@dimen/smallMargin"
        android:visibility="visible">

               <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_margin="5dp"
            android:layout_weight="1"
            android:scrollbars="vertical" />

        <LinearLayout
            android:id="@+id/giveawayResult"
            android:layout_width="match_parent"
            android:layout_height="@dimen/giveawayCardHeight"
            android:layout_marginBottom="@dimen/smallMargin"
            android:layout_marginTop="@dimen/smallMargin"
            android:visibility="invisible">

            <RelativeLayout
                android:layout_width="160dp"
                android:layout_height="@dimen/giveawayCardHeight">

                <ImageView
                    android:id="@+id/raffleThumbnail"
                    android:layout_width="160dp"
                    android:layout_height="match_parent"
                    android:scaleType="centerCrop"
                    android:src="@drawable/asd" />

                <TextView
                    android:id="@+id/raffleOwner"
                    android:layout_width="180dp"
                    android:layout_height="wrap_content"

                    android:layout_alignParentBottom="true"
                    android:alpha="0.7"
                    android:background="@color/colorPrimaryDark"
                    android:gravity="center_horizontal|bottom"
                    android:text="viajandocomgabi"
                    android:textColor="@color/cardview_light_background"
                    android:textSize="16sp" />
            </RelativeLayout>

            <LinearLayout

                android:layout_width="match_parent"
                android:layout_height="match_parent"

                android:layout_marginLeft="@dimen/smallMargin"

                android:orientation="vertical">

                <EditText
                    android:id="@+id/raffleDate"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="left"

                    android:focusable="false"
                    android:hint="@string/raffle_date_label"
                    android:inputType="date"
                    android:longClickable="false"
                    android:textSize="@dimen/discreteFont" />


                <EditText
                    android:id="@+id/amountToIndicate"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="left"

                    android:hint="@string/prompt_friends_to_indicate"
                    android:inputType="number"
                    android:lines="1"
                    android:textSize="@dimen/discreteFont" />

                <Space
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="@string/save" />
            </LinearLayout>
        </LinearLayout>


    </LinearLayout>

=========更新==========

为了清楚起见:
1-动态添加
adview 2-recycleview是0dp因为我希望它填充左侧空间

3-膨胀布局后,android api提到了recycleview,之后我添加了adview,它没有调整大小,所以结果就是你在照片上看到的

我需要知道如何强制 android redimensionate recycleview 缩小以适应空间

标签: androidandroid-linearlayoutdimensionsinvalidation

解决方案


要刷新 LinearLayout 或任何视图,请使用它的invalidate()方法。

编辑:如果您想重新测量视图,请调用它的requestLayout()方法。不要忘记也使它无效。

此外,您的 Admob 横幅广告违反了他们的政策。横幅广告不能与应用内容重叠。


推荐阅读