我正在开发一个货币转换器应用程序。一切正常。我在 xml 文件中使用嵌套线性和相对布局。我已经将相对布局中的布局一个一个地排列在另一个之上,如下所示。 (简单的想法)
--------------相对布局----------
------第一行------
------分隔线-----
-----第二行------
--------------/RelativeLayout/---------
我在 xml 中设置了 firstRow
下面的 divider
,然后设置了 divider
下面的 secondRow
。现在我想像这样重新排列行。
--------------相对布局----------
------第二行------
------分隔线-----
-----第一行------
--------------/RelativeLayout/---------
我正在尝试执行 secondRow
下方的 divider
和 divider
下方的 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/