为什么View的边距在GONE时仍会影响布局?

时间:2017-07-20 16:13:10

标签: android android-layout

我内部有一个LinearLayout和一个FrameLayout。奇怪的是,即使visibility设置为GONE,内部视图的边距似乎也会对周围的元素产生影响。这只是运行时的情况,而不是在IDE中的设计时间。

Android文档声明:

  

GONE - 此视图不可见,并且没有任何空间用于布局目的

  1. 那么为什么保证金仍有影响?
  2. 它有什么意义(如果有的话)?
  3. 最重要的是:我如何完成一个完全消失并且对其邻居没有任何影响的观点?
  4. 当然,您可以在代码中将边距设置为0,但是当您必须经常切换可见性并保持正确的边距时,这真的很不方便。

    这是布局文件:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/cardLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
    
        <LinearLayout
            android:id="@+id/cardContentStack"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true">
    
            <ImageView
                android:id="@+id/cardImageLarge"
                android:layout_width="match_parent"
                android:layout_height="@dimen/card_image_big_height"
                android:layout_marginBottom="@dimen/card_image_big_bottom_margin"
                android:layout_marginLeft="@dimen/margin_base"
                android:layout_marginRight="@dimen/margin_base"
                android:visibility="gone"
                tools:src="@drawable/some_image"
                tools:visibility="visible" />
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
    
                <FrameLayout
                    android:id="@+id/cardImageWrapper"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="@dimen/margin_base">
    
                    <ImageView
                        android:id="@+id/cardImage"
                        android:layout_width="@dimen/card_image_size"
                        android:layout_height="@dimen/card_image_size"
                        android:visibility="gone"
                        tools:src="@drawable/some_image2"
                        tools:visibility="visible" />
    
                    <ImageView
                        android:id="@+id/cardImageSmall"
                        android:layout_width="@dimen/card_image_small_size"
                        android:layout_height="@dimen/card_image_small_size"
                        android:layout_margin="@dimen/margin_base"
                        android:visibility="gone"
                        tools:src="@drawable/some_image3"
                        tools:visibility="visible" />
    
                </FrameLayout>
    
                <LinearLayout
                    android:id="@+id/cardBodyContainer"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">
    
                    <LinearLayout
                        android:id="@+id/cardTextContainer"
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="1"
                        android:orientation="vertical"
                        android:paddingBottom="@dimen/margin_base"
                        android:paddingRight="@dimen/margin_base"
                        android:paddingTop="@dimen/card_title_top_margin">
    
                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="horizontal">
    
                            <com.company.android.text.textview.BodyMedium
                                android:id="@+id/cardTitle"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_weight="1"
                                android:textColor="@color/midnight"
                                tools:text="Michele Theisen" />
    
                            <FrameLayout
                                android:id="@+id/cardTitleRightSpacer"
                                android:layout_width="@dimen/card_title_right_spacer"
                                android:layout_height="match_parent"
                                android:layout_weight="1"
                                android:visibility="gone"
                                tools:visibility="visible">
    
                            </FrameLayout>
                        </LinearLayout>
    
                        <com.company.android.text.textview.BodyLittle
                            android:id="@+id/cardBody"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="@dimen/card_title_top_margin"
                            android:layout_weight="1"
                            android:ellipsize="end"
                            android:maxLines="2"
                            android:textColor="@color/twilight"
                            android:visibility="gone"
                            tools:text="Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text"
                            tools:visibility="visible" />
                    </LinearLayout>
    
                    <LinearLayout
                        android:id="@+id/cardButtonContainer"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="right"
                        android:orientation="horizontal"
                        android:paddingBottom="@dimen/margin_small"
                        android:paddingLeft="@dimen/margin_small"
                        android:paddingRight="@dimen/margin_small"
                        android:visibility="gone"
                        tools:visibility="visible">
    
                        <com.company.views.materialstextviews.CardButton
                            android:id="@+id/cardSecondaryButton"
                            android:layout_width="0dp"
                            android:layout_height="@dimen/card_action_button_height"
                            android:layout_marginRight="@dimen/margin_small"
                            android:layout_weight="1"
                            android:background="?android:attr/selectableItemBackground"
                            android:maxLines="1"
                            android:paddingLeft="@dimen/margin_xs"
                            android:paddingRight="@dimen/margin_xs"
                            android:singleLine="true"
                            android:textColor="@color/twilight"
                            android:visibility="gone"
                            tools:text="BUTTON TEXT MIGHT BE LONG"
                            tools:visibility="visible" />
    
                        <com.company.views.materialstextviews.CardButton
                            android:id="@+id/cardPrimaryButton"
                            android:layout_width="wrap_content"
                            android:layout_height="@dimen/card_action_button_height"
                            android:background="?android:attr/selectableItemBackground"
                            android:ellipsize="end"
                            android:maxLines="1"
                            android:paddingLeft="@dimen/margin_xs"
                            android:paddingRight="@dimen/margin_xs"
                            android:singleLine="true"
                            android:textColor="@color/green"
                            android:visibility="visible"
                            tools:text="BUTTON TEXT"
                            tools:visibility="visible" />
    
                    </LinearLayout>
    
                </LinearLayout>
    
            </LinearLayout>
    
        </LinearLayout>
    
        <ImageView
            android:id="@+id/cardCloseButton"
            android:layout_width="@dimen/card_close_button_size"
            android:layout_height="@dimen/card_close_button_size"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:background="@drawable/ic_card_close"
            android:clickable="true"
            android:foreground="?android:attr/selectableItemBackground" />
    
    
    </RelativeLayout>
    

    cardImageWrapper是导致问题的视图。

0 个答案:

没有答案