Android 以编程方式移动视图
我有一个imageView,我想在特定情况下移动它 最初我有一个相对布局,有两个TextView和一个imageview。文本视图的方向是一个在另一个之上。已设置imageViewAndroid 以编程方式移动视图,android,textview,imageview,android-relativelayout,Android,Textview,Imageview,Android Relativelayout,我有一个imageView,我想在特定情况下移动它 最初我有一个相对布局,有两个TextView和一个imageview。文本视图的方向是一个在另一个之上。已设置imageView android:layout_below="@id/text_view1" android:layout_toEndOf="@+id/text_view2". 在逻辑文本中,当满足特定条件时,将删除_view2。当满足此条件时,我希望以编程方式将imageView移动到tex
android:layout_below="@id/text_view1"
android:layout_toEndOf="@+id/text_view2".
在逻辑文本中,当满足特定条件时,将删除_view2。当满足此条件时,我希望以编程方式将imageView移动到text_view1的末尾。基本上,当删除text_view2时,我想将imageView设置为
android:layout_toEndOf="@+id/text_view1"
我不认为在这里设置X、Y、Z值是合适的,因为以编程方式,我不知道给定不同的屏幕大小和密度,imageView将显示在哪里。我只需要将它移到第一个文本视图的末尾。看一看。您需要在布局参数中操作布局规则,如下所示:
// Make textView2 invisible
tv2.visibility = View.INVISIBLE
// Get the LayoutParams of the ImageView
val ivParams = iv.layoutParams as RelativeLayout.LayoutParams
// Change the rule to be to the right of textView1
ivParams.addRule(RelativeLayout.RIGHT_OF, tv1.id)
// Since the placement of textView2 is changing, request a layout.
iv.requestLayout()
考虑使用“END_OF”而不是“RIGHT_OF”。您可以将
视图
s放置在嵌套的线性布局中
或使用带有屏障的约束视图
通常建议使用ConstraintLayout
,因为嵌套的LinearLayout
s对性能有害,但由于ConstraintLayout
需要一些时间才能适应,我不想忽略另一个选项
为了演示这两种方法,我在同一屏幕中设置了一个小示例,其中包含LinearLayout
和ConstraintLayout
:
我不了解所有的上下文和设计,但我认为使用ConstraintLayout很容易做到这一点,你能用初始情况和预期情况展示设计吗?我建议不要使用RelativeLayout:要么将两个文本视图放在一个(垂直)线性布局中,要么将其与另一个(水平)线性布局中的ImageView放在一起。或者使用ConstraintLayout:虽然不容易理解,但您可以在一个视图组中实现所有功能。在您的情况下,a将完成trick@BömachtBlau前两个线性布局将是一场噩梦,试图将imageView放置在正确的位置几乎是不可能的。此外,屏障设计用于防止视图超出屏幕上的特定平面。除非你能帮助我如何使用屏障,否则我认为这没有多大帮助。我已经考虑过使用ConstraintLayout。我正在试图找出如何将图像移动到上面这行的末尾。如果对特定视图有约束且该视图被指定为“已消失”,则受约束视图将跳转到屏幕的左上角。。。ie没有约束,我不得不添加一个额外的addRule来将图像放在正确的位置,但这很好,谢谢!
<FrameLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.fragment.TabTwoFragment">
<LinearLayout
android:layout_width="240dp"
android:layout_height="128dp"
android:background="#cccccc">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#44ff0000"
android:maxWidth="160dp"
android:text="Upper TextView\nin\n nested\n LinearLayout" />
<TextView
android:id="@+id/longTextViewInLinearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#4400ff00"
android:maxWidth="160dp"
android:text="Lower TextView in nested LinearLayout" />
</LinearLayout>
<ImageView
android:id="@+id/linearLayoutImageView"
android:layout_width="48dp"
android:layout_height="48dp"
android:background="#ffffff"
android:src="@drawable/ic_android_black_24dp"
android:tint="@color/colorPrimary" />
</LinearLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="240dp"
android:layout_height="128dp"
android:layout_gravity="bottom|end"
android:background="#666666">
<TextView
android:id="@+id/shortTextViewInConstraintLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#44ff0000"
android:maxWidth="160dp"
android:text="Upper TextView\nin\n nested\nConstraintLayout"
android:textColor="#ffffff"
app:layout_constraintBottom_toTopOf="@+id/longTextViewInConstraintLayout"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:layout_constraintVertical_chainStyle="packed" />
<TextView
android:id="@+id/longTextViewInConstraintLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#4400ff00"
android:maxWidth="160dp"
android:text="Lower TextView in ConstraintLayout"
android:textColor="#ffffff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/shortTextViewInConstraintLayout" />
<androidx.constraintlayout.widget.Barrier
android:id="@+id/barrier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="end"
app:constraint_referenced_ids="shortTextViewInConstraintLayout, longTextViewInConstraintLayout" />
<ImageView
android:id="@+id/constraintLayoutImageView"
android:layout_width="48dp"
android:layout_height="48dp"
android:background="#ffffff"
android:importantForAccessibility="no"
android:src="@drawable/ic_android_black_24dp"
android:tint="@color/colorAccent"
app:layout_constraintStart_toEndOf="@+id/barrier"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TransitionSet ts = new TransitionSet();
ts.addTransition( new ChangeBounds());
ts.addTransition(new Slide());
View imageViewInLinearLayout = view.findViewById(R.id.linearLayoutImageView);
imageViewInLinearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TransitionManager.beginDelayedTransition((ViewGroup)getView(), ts);
view.findViewById(R.id.longTextViewInLinearLayout).setVisibility(GONE);
}
});
View imageViewInConstraintLayout = view.findViewById(R.id.constraintLayoutImageView);
imageViewInConstraintLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TransitionManager.beginDelayedTransition((ViewGroup)getView(), ts);
view.findViewById(R.id.longTextViewInConstraintLayout).setVisibility(GONE);
}
});
}