【问题标题】:How to order elements inside a Linear Layout?如何在线性布局中对元素进行排序?
【发布时间】:2016-07-05 18:20:04
【问题描述】:

我有一个应用程序,它在 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>

【问题讨论】:

    标签: android android-linearlayout


    【解决方案1】:

    为您的 LinearLayout 设置一个 id:

    android:id="@+id/myLinearLayout
    

    在 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.
    }
    

    看看Droid: How to reorder a linearlayouts contents programmatically?Get all child views inside LinearLayout at once

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多