Android:重新计算布局组件的尺寸

时间:2018-07-13 22:52:45

标签: android android-linearlayout dimensions invalidation

我的线性布局中的组件很少,大多数组件都设置了特定尺寸,但其他组件中的0dp设置为1来填充剩余空间。

问题是在xml布局膨胀后,我以编程方式添加了一个新组件,而0dp视图并没有缩小以适应此新组件,因此使布局显示错误。

所有组件都有足够的空间,例如,如果我尝试在xml上添加“ dinamic”组件(仅用于测试)并将其充气,则一切都会很好。

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

我得到什么: enter image description here
代码如下:

<?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重新定义recycleview使其缩小以适应空间

1 个答案:

答案 0 :(得分:0)

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

编辑:如果要重新测量视图,请调用它的requestLayout()方法。别忘了使它失效。

此外,您的Admob标语广告违反了其政策。横幅广告不能在应用内容上重叠。

相关问题