问题描述
一个是autoCompleteTextView
,另一个是EditText
。
基于用户单击的按钮,它使用setVisibility(View.GONE)/setVisibility(View.VISIBLE)
更改了这些对象的可见性。
在我的默认方案中,一个ImageButton
对象放置在我的xml中EditText
的末尾。
但是,当用户单击以使用autoCompleteTextView
时,ImageButton
会跳到其他位置。
由于所有ImageButtons/EditText/autoCompleteTextView
都放置在RelativeLayout中,因此我尝试通过以下方式以编程方式固定ImageButton
的位置:
RelativeLayout.LayoutParams params= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_END,R.id.relativeLayout);
params.addRule(RelativeLayout.ALIGN_TOP,R.id.relativeLayout);
params.addRule(RelativeLayout.ALIGN_BottOM,R.id.relativeLayout);
ib_Clear.setLayoutParams(params);
我的xml在哪里:
<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="wrap_content"
android:layout_height="66dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/spi_method">
<AutoCompleteTextView
android:id="@+id/ATV_Search"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="20dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="20dp"
android:background="@drawable/et_rounded"
android:dropDownHeight="100dp"
android:fontFamily="@font/assistant"
android:hint="@string/ActivitySearchItems_Search"
android:imeOptions="actionSearch"
android:inputType="text"
android:paddingStart="56dp"
android:paddingEnd="64dp"
android:textColorHint="@color/colorGray"
android:textSize="14sp"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/Et_Search"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="20dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="20dp"
android:background="@drawable/et_rounded"
android:fontFamily="@font/assistant"
android:hint="@string/ActivitySearchItems_Search"
android:imeOptions="actionSearch"
android:inputType="text"
android:paddingStart="56dp"
android:paddingEnd="64dp"
android:textColorHint="@color/colorGray"
android:textSize="14sp"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="@+id/Ib_Clear"
android:layout_width="24dp"
android:layout_height="match_parent"
android:layout_alignEnd="@id/Et_Search"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:background="@android:color/transparent"
android:src="@drawable/ic_cancel" />
<ImageButton
android:id="@+id/Ib_Search"
android:layout_width="48dp"
android:layout_height="match_parent"
android:layout_alignStart="@id/Et_Search"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:background="@android:color/transparent"
android:src="@drawable/ic_Items_search" />
</RelativeLayout>
即使将Ib_Clear ImageButton
设置为GONE,我还有其他方法可以将Et_Search
保持在其位置吗?我也找不到在设置中添加边距的方法。
谢谢
解决方法
在XML文件中使用“框架布局”包围要隐藏的视图,以使“框架布局”成为要隐藏的视图的父级。然后只需隐藏视图的可见性即可,这不会影响视图的跳跃。
,尝试使用约束布局,请参考此链接以了解更多https://developer.android.com/training/constraint-layout
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginStart="20dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/spi_method">
<AutoCompleteTextView
android:id="@+id/ATV_Search"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/apple"
android:dropDownHeight="100dp"
android:imeOptions="actionSearch"
android:inputType="text"
android:textSize="14sp"
app:layout_constraintEnd_toStartOf="@+id/Ib_Clear"
app:layout_constraintStart_toEndOf="@+id/Ib_Search"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/Et_Search"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/alto"
android:imeOptions="actionSearch"
android:inputType="text"
android:textSize="14sp"
app:layout_constraintEnd_toStartOf="@+id/Ib_Clear"
app:layout_constraintStart_toEndOf="@+id/Ib_Search"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="@+id/Ib_Clear"
android:layout_width="24dp"
android:layout_height="match_parent"
android:layout_alignEnd="@id/Et_Search"
android:background="@android:color/transparent"
android:src="@color/attachment_icon_pdf"
app:layout_constraintEnd_toEndOf="@+id/ATV_Search" />
<ImageButton
android:id="@+id/Ib_Search"
android:layout_width="48dp"
android:layout_height="match_parent"
android:layout_alignStart="@id/Et_Search"
android:background="@android:color/transparent"
android:src="@color/black"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
,
如果您只想隐藏视图(并保留其他所有内容的位置,为什么不将可见性更改为INVISIBLE而不是GONE?)
不可见:
此视图是不可见的,但仍会占用布局空间 目的。
消失:
此视图不可见,并且不占用任何空间进行布局 目的。