如何在线性布局内排序元素? [英] How to order elements inside a Linear Layout?

查看:81
本文介绍了如何在线性布局内排序元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an application that shows different images inside a LinearLayout (which is inside a ScrollView).

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

I would like to reorder the elements in the Layout when the user press a button. For example: If I have |pic1|pic2|pic3| shown in the layout but I want to reorder them so they appear as |pic2|pic1|pic3|

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

This is my "activity_main.xml" file where I have all my images:

<?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>

推荐答案

为您的LinearLayout设置一个ID:

set an id to your LinearLayout:

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:如何以编程方式对线性布局内容重新排序?/a>和一次在LinearLayout中获取所有子视图

这篇关于如何在线性布局内排序元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆