Android 如何动态更改线性布局中静态创建的布局的顺序?(安卓工作室)
我用XML代码创建了一个布局。它有一个Android 如何动态更改线性布局中静态创建的布局的顺序?(安卓工作室),android,android-layout,Android,Android Layout,我用XML代码创建了一个布局。它有一个滚动视图,在该滚动视图中有两个相对的布局 布局如下: <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app=&q
滚动视图
,在该滚动视图中有两个相对的布局
布局如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#151515">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_10sdp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:onClick="monday"
android:id="@+id/monday"
android:layout_width="match_parent"
android:layout_height="@dimen/_145sdp"
app:srcCompat="@drawable/mondaysmolgray" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="@dimen/_70sdp"
android:layout_marginBottom="0dp"
android:orientation="horizontal"
android:weightSum="3">
<TextView
android:id="@+id/mondaydate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/_18sdp"
android:layout_weight="1"
android:gravity="center"
android:text=" No tasks"
android:textColor="#151515"
android:textSize="@dimen/_15sdp"></TextView>
<TextView
android:id="@+id/mondaytasks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/_18sdp"
android:layout_weight="1"
android:gravity="center"
android:text=" No tasks"
android:textColor="#151515"
android:textSize="@dimen/_15sdp"></TextView>
<TextView
android:id="@+id/mondaytodos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/_18sdp"
android:layout_weight="1"
android:gravity="center"
android:text=" No tasks"
android:textColor="#151515"
android:textSize="@dimen/_15sdp"></TextView>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:onClick="tuesday"
android:id="@+id/tuesday"
android:layout_width="match_parent"
android:layout_height="@dimen/_145sdp"
app:srcCompat="@drawable/tuesdaysmolgray" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="@dimen/_70sdp"
android:layout_marginBottom="0dp"
android:orientation="horizontal"
android:weightSum="3">
<TextView
android:id="@+id/tuesdaydate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/_18sdp"
android:layout_weight="1"
android:gravity="center"
android:text=" No tasks"
android:textColor="#151515"
android:textSize="@dimen/_15sdp"></TextView>
<TextView
android:id="@+id/tuesdaytasks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/_18sdp"
android:layout_weight="1"
android:gravity="center"
android:text=" No tasks"
android:textColor="#151515"
android:textSize="@dimen/_15sdp"></TextView>
<TextView
android:id="@+id/tuesdaytodos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/_18sdp"
android:layout_weight="1"
android:gravity="center"
android:text=" No tasks"
android:textColor="#151515"
android:textSize="@dimen/_15sdp"></TextView>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
正如您所看到的,在滚动视图中有两个相对的布局。
我想动态地改变两个线性布局的顺序,而无需在运行时代码中再次创建整个布局。我该怎么做呢?android布局中没有重新排序方法 因此,您可以尝试使用
removeView(view)
删除所有视图
然后您需要按照您喜欢的顺序使用addView(childView,index)
添加它们
在您的情况下,需要定义父LinearLayout和两个RelativeLayout的id
<ScrollView>
<LinearLayout android:id="@+id/container">
<RelativeLayout android:id="@+id/first">
<RelativeLayout android:id="@+id/second">
</LinearLayout>
</ScrollView>
android布局中没有重新排序方法 因此,您可以尝试使用
removeView(view)
删除所有视图
然后您需要按照您喜欢的顺序使用addView(childView,index)
添加它们
在您的情况下,需要定义父LinearLayout和两个RelativeLayout的id
<ScrollView>
<LinearLayout android:id="@+id/container">
<RelativeLayout android:id="@+id/first">
<RelativeLayout android:id="@+id/second">
</LinearLayout>
</ScrollView>