java - 在android中通过java代码重新排列RelativeLayout子级

标签 java android xml android-layout

我正在开发一个货币转换器应用程序。一切正常。我在 xml 文件中使用嵌套线性和相对布局。我已经将相对布局中的布局一个一个地排列在另一个之上,如下所示。 (简单的想法)

--------------相对布局----------

------第一行------

------分隔线-----

-----第二行------

--------------/RelativeLayout/---------

我在 xml 中设置了 firstRow 下面的 divider,然后设置了 divider 下面的 secondRow。现在我想像这样重新排列行。

--------------相对布局----------

------第二行------

------分隔线-----

-----第一行------

--------------/RelativeLayout/---------

我正在尝试执行 secondRow 下方的 dividerdivider 下方的 firstRow 操作。但它给了我循环依赖错误。我了解所有关于循环依赖项的知识,并且我相信java代码不会重置上述布局参数,而是类似于合并两个布局,这肯定会引发循环依赖项错误。

XML CODE

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:id="@+id/firstRow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:orientation="horizontal">

        <LinearLayout
            android:id="@+id/namelayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/img"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:background="@drawable/ic_sampleicon" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_margin="@dimen/activity_horizontal_margin"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/name"
                    style="@style/TextLableMyTheme"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="" />

                <TextView
                    android:id="@+id/brand"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=""
                    android:textColor="#f7f7f7" />
            </LinearLayout>

        </LinearLayout>

        <TextView

            android:id="@+id/tvpoints"
            style="@style/TextCurrencyMyTheme"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:text="" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/divider"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/firstRow"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:orientation="horizontal">

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_centerInParent="true"
            android:background="@drawable/background_gradient_light" />

        <ImageView
            android:id="@+id/switcherImg"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_centerInParent="true"
            android:src="@drawable/ic_settings"
            android:tint="#f7f7f7" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/secondRow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/divider"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <ImageView
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:src="@drawable/ic_cash"
                android:tint="#ffe100" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_margin="@dimen/activity_horizontal_margin"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/name2"
                    style="@style/TextLableMyTheme"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Riyal" />

                <TextView
                    android:id="@+id/brand2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=""
                    android:textColor="#f7f7f7" />
            </LinearLayout>

        </LinearLayout>

        <TextView

            android:id="@+id/tv"
            style="@style/TextCurrencyMyTheme"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:text="0.0" />
    </RelativeLayout>
</RelativeLayout>

Java Code to update params

    RelativeLayout.LayoutParams _firstRowParams = 
   (RelativeLayout.LayoutParams)_firstRow.getLayoutParams();
    _firstRowParams.addRule(RelativeLayout.BELOW, R.id.divider);

    RelativeLayout.LayoutParams _dividerParams = 
   (RelativeLayout.LayoutParams)_divider.getLayoutParams();
    _dividerParams.addRule(RelativeLayout.BELOW, R.id.secondRow);

   /* _firstRow.setLayoutParams(_firstRowParams);
    _divider.setLayoutParams(_dividerParams);*/

最佳答案

尝试添加以下代码:

RelativeLayout.LayoutParams _secondRowParams = 
   (RelativeLayout.LayoutParams)_secondRow.getLayoutParams();
    _secondRowParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
_secondRowParams.removeRule(RelativeLayout.BELOW);

尝试使用上面的代码。这对我有用。您需要删除之前的 Below 规则以避免循环依赖。

关于java - 在android中通过java代码重新排列RelativeLayout子级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43447309/

相关文章:

android - 如何在 Android 中混淆字符串?

Android Build from source find - 没有这样的文件目录错误

xml - XPath 和 Jabber-RPC

java - 将 org.wc3.dom.Element Java 对象写入 javax.xml.stream.XMLStreamWriter 的最佳方法是什么?

java - 使用 Java 将 EPUB 转换为 PDF

java - 使用hibernate 4.2.0需要哪些库?

java - 处理多个流中的所有异常

java - 使用具有默认值的 Quartz 调度程序的计划作业

android - GoogleApiClient onConnected 从未在可穿戴设备上调用

xml - xslt 按子元素计数排序