我在RelativeLayout中有一个TextView(宽度:fill_parent),我想确保它位于其上方并排的两个 View 下方,具有动态宽度和高度。这意味着,有时左 View 较高,有时右 View 较高。
我尝试设置两个“以下”参数,但这当然是不允许的。 我尝试通过代码修改它(XML将其设置在txt TextView下方,img是其旁边的ImageView):
if (txt.getHeight() + txt.getTop() < img.getHeight() + img.getTop()) {
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params.addRule(RelativeLayout.BELOW, R.id.image);
txtDetails.setLayoutParams(params);
}
(txtDetails 是我想要在两个 View 下方的那个)。
当这不起作用(没有任何改变)时,我将 txtDetails 放在它自己的relativelayout中,位于包含两个 View 并且有效的relativelayout下面。不过,我觉得 RL 应该能够处理这种情况,而无需创建新的 RL。是否可以?或者定位此 View 的最佳方式是什么?
谢谢
最佳答案
为什么不使用表格布局?它在两行中完全满足您的要求。
首先在一行中添加两个变量 View ,然后添加包含 TextView 的另一行。
像这样:
<TableLayout
android:id="@+id/center"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableRow >
<TextView
android:id="@+id/textView1"
android:layout_width="150sp"
android:layout_height="wrap_content"
android:text="@string/lorem_short" />
<TextView
android:id="@+id/textView2"
android:layout_width="150sp"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:text="@string/lorem_long" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/textView3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5sp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
</TableLayout>
关于android - relativelayout - 将项目定位在两个 View 中最低的一个下方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10071845/