如何对线性布局中的元素进行排序?

Alo*_*nso 1 android android-linearlayout

我有一个应用程序,可以在 LinearLayout (位于 ScrollView 内部)内显示不同的图像。

当用户按下按钮时,我想对布局中的元素重新排序。例如:如果我有 |pic1|pic2|pic3| 显示在布局中,但我想重新排序它们,使它们显示为 |pic2|pic1|pic3|

这是我的“activity_main.xml”文件,其中包含所有图像:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/map"
    tools:context="dis2.widget.MainActivity">

    <HorizontalScrollView
        android:layout_width="fill_parent"
        android:layout_height="80dp"
        android:id="@+id/contactsScrollView"
        android:fillViewport="false"
        android:visibility="visible"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="26dp">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="visible">

            <ImageView
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:src="@drawable/pic1"
                android:id="@+id/pic1ID"
                android:visibility="gone" />

            <ImageView
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:src="@drawable/pic2"
                android:id="@+id/pic2ID"
                android:visibility="gone" />

            <ImageView
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:src="@drawable/pic3"
                android:id="@+id/pic3ID"
                android:visibility="visible" />

        </LinearLayout>
    </HorizontalScrollView>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Reorder"
        android:id="@+id/reorderB"
        android:layout_marginBottom="114dp"
        android:onClick="reorder"
        android:layout_alignParentBottom="true" />


</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

Luc*_*r10 5

为您的 LinearLayout 设置一个 id:

android:id="@+id/myLinearLayout
Run Code Online (Sandbox Code Playgroud)

在Java中:

public void reorder() {

    LinearLayout myLinearLayout = (LinearLayout) findViewById(R.id.myLinearLayout);
    // get number of children
    int childcount = myLinearLayout.getChildCount();
    // create array
    View[] children = new View[childcount];

    // get children of linearlayout 
    for (int i=0; i < childcount; i++){
      children[i] = myLinearLayout.getChildAt(i);
    }

    //now remove all children
    myLinearLayout.removeAllViews();

    //and resort, first position
    myLinearLayout.addView(children[2]);
    //second position
    myLinearLayout.addView(children[0]);
    //etc.
}
Run Code Online (Sandbox Code Playgroud)

看看Droid:如何以编程方式重新排序线性布局内容?立即获取 LinearLayout 内的所有子视图