我在Fragment
上有此布局。它有3个Buttons
。在Button3
下有一个TextView
。设置了此布局后,我正在检查某些条件。如果满足该条件,我将显示Button3
。但是如果不满足条件,我就需要隐藏按钮。
这是隐藏Button3
当隐藏按钮时,TextView
和Button2
之间似乎有很多空白,但是在我的XML布局中,它在那里。如何避免这种情况,以免出现空白?
这是XML的代码
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="@+id/myConstraint"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/allscreenbackground">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/tabIndicator"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="Table: "
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBaseline_toBaselineOf="@+id/tableName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.03"
app:layout_constraintStart_toStartOf="@+id/toolbar2" />
<TextView
android:id="@+id/tableName"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:text=""
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBaseline_toBaselineOf="@+id/full_name"
app:layout_constraintEnd_toEndOf="@+id/toolbar2"
app:layout_constraintHorizontal_bias="0.04"
app:layout_constraintStart_toEndOf="@+id/name" />
<TextView
android:id="@+id/full_name"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="10dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ellipsize="marquee"
android:gravity="center"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:text=""
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/toolbar2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/tableName"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="28dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="0dp"
android:text="Pax -"
android:textColor="@color/textTitles"
android:textSize="18sp"
app:layout_constraintBaseline_toBaselineOf="@+id/people"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/guideline4" />
<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Occupied Since :"
android:textColor="@color/textTitles"
android:textSize="18sp"
app:layout_constraintBaseline_toBaselineOf="@+id/lapsedTime"
app:layout_constraintStart_toStartOf="@+id/guideline4" />
<TextView
android:id="@+id/people"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text=""
android:textColor="@color/textTitles"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/textView9"
app:layout_constraintTop_toBottomOf="@+id/toolbar2"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/lapsedTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text=""
android:textColor="@color/textTitles"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.06"
app:layout_constraintStart_toEndOf="@+id/textView10"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.18" />
<Button
android:id="@+id/shift"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="@drawable/yes_button"
android:gravity="center"
android:text="Button 1"
android:textColor="#fff"
android:textSize="15dp"
android:textStyle="bold"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.283" />
<Button
android:id="@+id/end"
android:layout_width="200dp"
android:layout_height="35dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="@drawable/yes_button"
android:text="Button 2"
android:textColor="#fff"
android:textSize="15dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.387" />
<android.support.constraint.Guideline
android:id="@+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="40dp" />
<Button
android:id="@+id/freeThisTable"
android:layout_width="200dp"
android:layout_height="35dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="@drawable/yes_button"
android:text="Button 3"
android:textColor="#fff"
android:textSize="15dp"
android:textStyle="bold"
android:visibility=""
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/isBday"
android:visibility="invisible"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/guideline4"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.65"
app:srcCompat="@drawable/birthday" />
<ImageView
android:visibility="invisible"
android:id="@+id/isAniversary"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/guideline4"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.78"
app:srcCompat="@drawable/ring_new" />
<TextView
android:visibility="visible"
android:id="@+id/isBdayText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:text="Hello"
android:textSize="18sp"
android:textColor="@color/textTitles"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/guideline32"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.64" />
<TextView
android:visibility="invisible"
android:id="@+id/isAniversaryText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
android:textSize="18sp"
android:textColor="@color/textTitles"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/guideline32"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.76" />
<android.support.constraint.Guideline
android:id="@+id/guideline32"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="100dp" />
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:0)
将此添加到您的.XML文件中
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_text"
android:visibility="gone" //Make sure put this in your xml to hide button and call that button in condition
android:id="@+id/hidebtn"
/>
然后更改条件的可见性
if(//Your Condition)
{
hidebtn.setVisibility(View.VISIBLE);
}
答案 1 :(得分:0)
如果要隐藏对象的空间,请使用它:
B
答案 2 :(得分:0)
问题是由于约束不正确。您已将大多数视图的TOP约束为父级,并设置了偏差以使其对齐。而是在连续的视图下对齐。它将按您的要求工作。
为了更好地理解,我已经编辑了您的xml文件。更换并签出。
<........./>
<android.support.constraint.Guideline
android:id="@+id/guideline4"
.............../>
<Button
android:id="@+id/freeThisTable"
android:layout_width="200dp"
android:layout_height="35dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:background="@drawable/yes_button"
android:text="Button 3"
android:textColor="#fff"
android:textSize="15dp"
android:textStyle="bold"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/end" />
<ImageView
android:id="@+id/isBday"
android:layout_width="40dp"
android:layout_height="40dp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="@+id/isBdayText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/guideline4"
app:layout_constraintTop_toTopOf="@+id/isBdayText"
app:srcCompat="@drawable/birthday" />
<ImageView
android:id="@+id/isAniversary"
android:layout_width="40dp"
android:layout_height="40dp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="@+id/isAniversaryText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/guideline4"
app:layout_constraintTop_toTopOf="@+id/isAniversaryText"
app:srcCompat="@drawable/ring_new" />
<TextView
android:id="@+id/isBdayText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Hello"
android:textColor="@color/textTitles"
android:textSize="18sp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/guideline32"
app:layout_constraintTop_toBottomOf="@+id/freeThisTable" />
<TextView
android:id="@+id/isAniversaryText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="TextView"
android:textColor="@color/textTitles"
android:textSize="18sp"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/guideline32"
app:layout_constraintTop_toBottomOf="@+id/isBdayText" />
<android.support.constraint.Guideline
android:id="@+id/guideline32"
.............. />