以编程方式反向RelativeLayout内部的布局

时间:2017-02-22 11:03:57

标签: android android-relativelayout android-viewgroup

我在RelativeLayout内有一个relativeLayout和三个视图。视图彼此相对。

    <RelativeLayout
    android:id="@+id/includelayoutgroup"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/transparentColorView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:clickable="true"
        android:visibility="visible" />


    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/keyboard_main_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:visibility="visible">

        <include
            layout="@layout/transfer_keyboard_fragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </RelativeLayout>


    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/finalRequestView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:clickable="true"
        android:visibility="visible">

        <include
            layout="@layout/fragment_transfer_new_version_container_child"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </RelativeLayout>
</RelativeLayout>

是否可以反转我的观点?我的意思是,在我的代码中,最后一个布局是finalRequestView,在按钮单击中,我会在第一个位置看到我的上一个视图,在我的视图中看到其他视图的位置, 我怎么能解决这个问题?

1 个答案:

答案 0 :(得分:0)

我尝试按照demo&amp; amp;它可以按你的需要工作,请看看

enter image description here

ReverseLayout.java

public class ReverseLayout extends AppCompatActivity {

    LinearLayout llRoot;
    Button btReverseLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reverse_layout);

        llRoot = (LinearLayout) findViewById(R.id.llRoot);
        btReverseLayout = (Button) findViewById(R.id.btReverseLayout);

        btReverseLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<View> alViews = new ArrayList<View>();
                for (int i = llRoot.getChildCount() - 1; i >= 0; i--) {
                    View view = llRoot.getChildAt(i);
                    llRoot.removeViewAt(i);
                    alViews.add(view);
                }

                for (int j = 0; j < alViews.size(); j++) {
                    llRoot.addView(alViews.get(j));
                }
            }
        });
    }
}

reverse_layout.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:id="@+id/activity_6"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.tosc185.testproject.Activity6">

    <LinearLayout
        android:id="@+id/llRoot"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FF0000"
            android:gravity="center"
            android:orientation="vertical"
            android:padding="15dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="1"
                android:textSize="16dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#00FF00"
            android:gravity="center"
            android:orientation="vertical"
            android:padding="15dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2"
                android:textSize="16dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#0000FF"
            android:gravity="center"
            android:orientation="vertical"
            android:padding="15dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="3"
                android:textSize="16dp" />
        </LinearLayout>

    </LinearLayout>

    <Button
        android:id="@+id/btReverseLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Reverse Layout" />
</RelativeLayout>