当可见性设置为GONE时,删除LinearLayout之间的空格

时间:2017-02-12 11:31:13

标签: android android-layout layout

我有这个:

enter image description here

在这种情况下,右边的箭头是关闭的,我已将LinearLayout的Visibility模式设置为GONE,但是我与第一个LinearLayout和第二个LinearLayout有很多差距。 当我点击rigth箭头时,我将Visibility设置为VISIBLE,并且我有最大扩展。

enter image description here

代码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"
    xmlns:tools="http://schemas.android.com/tools"
    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="wrap_content"
        android:weightSum="100"
        android:baselineAligned="false"
        android:layout_marginTop="5dp">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="30"
            android:padding="5dp"
            android:weightSum="1">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:src="@drawable/directory_icon"
                android:id="@+id/img_product"
                android:scaleType="fitStart"
                android:layout_weight="1"
                tools:ignore="ContentDescription"
                />
        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="70"
            android:weightSum="100">

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_weight="30"
                android:layout_height="wrap_content"
                android:weightSum="100">

                <android.support.design.widget.TextInputLayout
                    android:id="@+id/input_title1"
                    android:layout_weight="90"
                    android:layout_height="wrap_content"
                    android:elevation="1dp"
                    android:layout_width="wrap_content">

                    <android.support.design.widget.TextInputEditText
                        android:id="@+id/edittitle1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="@string/txt_nome"
                        android:text=""
                        android:padding="5dp"
                        android:inputType="text"/>
                </android.support.design.widget.TextInputLayout>

                <ImageButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    app:srcCompat="@drawable/arrow_down"
                    android:id="@+id/arrow_expand"
                    android:layout_weight="10"
                    android:background="#00000000"
                    android:scaleType="centerInside"
                    android:layout_marginRight="5dp"
                    android:layout_marginLeft="5dp"
                    android:contentDescription=""
                    tools:ignore="ContentDescription"/>
            </LinearLayout>

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="70"
                android:visibility="gone"
                android:id="@+id/datacontainer">

                <android.support.design.widget.TextInputLayout
                    android:id="@+id/input_desc1"
                    android:layout_weight="90"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <android.support.design.widget.TextInputEditText
                        android:id="@+id/editdesc1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="@string/txt_descrizione2"
                        android:text=""
                        android:padding="5dp"
                        android:inputType="text"/>
                </android.support.design.widget.TextInputLayout>

                <android.support.design.widget.TextInputLayout
                    android:id="@+id/input_editor1"
                    android:layout_weight="90"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <android.support.design.widget.TextInputEditText
                        android:id="@+id/edit_carbo"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="@string/txt_carboidrati"
                        android:text=""
                        android:padding="5dp"
                        android:inputType="text|number|numberSigned|numberDecimal"/>
                </android.support.design.widget.TextInputLayout>

                <android.support.design.widget.TextInputLayout
                    android:id="@+id/input_editor3"
                    android:layout_weight="90"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <android.support.design.widget.TextInputEditText
                        android:id="@+id/edit_zuccheri"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="@string/txt_zuccheri"
                        android:text=""
                        android:padding="5dp"
                        android:inputType="text|number|numberSigned|numberDecimal"/>
                </android.support.design.widget.TextInputLayout>

                <android.support.design.widget.TextInputLayout
                    android:id="@+id/input_editor4"
                    android:layout_weight="90"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <android.support.design.widget.TextInputEditText
                        android:id="@+id/edit_grassi"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="@string/txt_grassi"
                        android:text=""
                        android:padding="5dp"
                        android:inputType="text|number|numberSigned|numberDecimal"/>
                </android.support.design.widget.TextInputLayout>


                <android.support.design.widget.TextInputLayout
                    android:id="@+id/input_editor5"
                    android:layout_weight="90"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <android.support.design.widget.TextInputEditText
                        android:id="@+id/edit_proteine"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="@string/txt_proteine"
                        android:text=""
                        android:padding="5dp"
                        android:inputType="text|number|numberSigned|numberDecimal"/>
                </android.support.design.widget.TextInputLayout>


                <android.support.design.widget.TextInputLayout
                    android:id="@+id/input_editor6"
                    android:layout_weight="90"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <android.support.design.widget.TextInputEditText
                        android:id="@+id/edit_fibre"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="@string/txt_fibre"
                        android:text=""
                        android:padding="5dp"
                        android:inputType="text|number|numberSigned|numberDecimal"/>
                </android.support.design.widget.TextInputLayout>


                <android.support.design.widget.TextInputLayout
                    android:id="@+id/input_editor7"
                    android:layout_weight="90"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <android.support.design.widget.TextInputEditText
                        android:id="@+id/edit_sale"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="@string/txt_sale"
                        android:text=""
                        android:padding="5dp"
                        android:inputType="text|number|numberSigned|numberDecimal"/>
                </android.support.design.widget.TextInputLayout>

                <android.support.design.widget.TextInputLayout
                    android:id="@+id/input_editor8"
                    android:layout_weight="90"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <android.support.design.widget.TextInputEditText
                        android:id="@+id/edit_kcal"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="@string/txt_kcal"
                        android:text=""
                        android:padding="5dp"
                        android:inputType="text|number|numberSigned|numberDecimal"/>
                </android.support.design.widget.TextInputLayout>

                <android.support.design.widget.TextInputLayout
                    android:id="@+id/input_editor9"
                    android:layout_weight="90"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <android.support.design.widget.TextInputEditText
                        android:id="@+id/edit_prezzo"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="@string/txt_prezzo"
                        android:text=""
                        android:padding="5dp"
                        android:inputType="text|number|numberSigned|numberDecimal"/>
                </android.support.design.widget.TextInputLayout>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

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

        <TextView
            android:text="@string/lista_allergeni"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/textView4"
            android:padding="5dp"/>

        <GridView
            android:id="@+id/myGrid"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp"
            android:verticalSpacing="2dp"
            android:horizontalSpacing="2dp"
            android:numColumns="4"
            android:columnWidth="60dp"
            android:stretchMode="columnWidth"
            android:gravity="center"
            android:textAlignment="center"/>
    </LinearLayout>
</LinearLayout>

当我点击时我使用这段代码:

....
    DataContainer = (LinearLayout) rootView.findViewById(R.id.datacontainer);
        IngContainer = (LinearLayout) rootView.findViewById(R.id.ingcontainer);
....

    ArrowExpand.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                if (DataContainer.getVisibility() == View.VISIBLE) {
                    // Its visible
                    DataContainer.setVisibility(View.GONE);
                    ObjectAnimator flip = ObjectAnimator.ofFloat(ArrowExpand, "rotationX", 180f, 0f);
                    flip.setDuration(500);
                    flip.start();
                } else {
                    // Either gone or invisible
                    DataContainer.setVisibility(View.VISIBLE);
                    ObjectAnimator flip = ObjectAnimator.ofFloat(ArrowExpand, "rotationX", 0f, 180f);
                    flip.setDuration(500);
                    flip.start();
                }
            }
        });

如何消除差距?我希望在第一个LinearLayout附近有第二个LinearLayout,并且只有当我点击箭头时我才有扩展。 也可以删除顶部的标题(MyViewRecip ...)?

THx的。

2 个答案:

答案 0 :(得分:0)

您可以先保存要删除的视图,然后删除此子项

答案 1 :(得分:0)

试着改变布局程序的重量

     LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, 100);
     lp.weight = 1;

       if (DataContainer.getVisibility() == View.VISIBLE) {
                        // Its visible
                        DataContainer.setVisibility(View.GONE);
    DataContainer.setLayoutParams(lp);
                        ObjectAnimator flip = ObjectAnimator.ofFloat(ArrowExpand, "rotationX", 180f, 0f);
                        flip.setDuration(500);
                        flip.start();
                    } else {
                        // Either gone or invisible
lp.weight = 70;
                        DataContainer.setVisibility(View.VISIBLE);
    DataContainer.setLayoutParams(lp);
                        ObjectAnimator flip = ObjectAnimator.ofFloat(ArrowExpand, "rotationX", 0f, 180f);
                        flip.setDuration(500);
                        flip.start();
                    }

我认为它可能会解决您的问题..