问题描述
我有一个如下所示的相对布局:
I have a relativeLayout like below:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:id="@+id/parent" >
<ListView
android:layout_width="360dp"
android:layout_height="600dp"
android:id="@+id/list"
android:inputType="text"
android:maxLines="1"
android:layout_margin="50dp"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
在java代码中,我想在listview的左边添加一个视图,但是没有用:
In the java code, I want to add a view to the left of the listview, but it didn't worked:
m_relativeLayout = (RelativeLayout)findViewById(R.id.parent);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.LEFT_OF, m_listView.getId());
Button button2 = new Button(this);
button2.setText("I am button 2");
m_relativeLayout.addView(button2, layoutParams);
仅当我将列表视图设置为 alignParentRight 时,它才会起作用.这是一个android错误还是我遗漏了什么?
only if I set the listview to alignParentRight, it will work. Is this an android bug or I'm missing something?
我总是尝试 addView(View child, int index, LayoutParams params)
,但它可能只适用于线性布局.那么是否有正常的解决方案可以使 RelativeLayout.LEFT_OF
工作?
I always try addView(View child, int index, LayoutParams params)
, but it might only work in the linearlayout. So is there an normal solution to make the RelativeLayout.LEFT_OF
work?
编辑
我已经尝试过 RelativeLayout.BELOW
和 RelativeLayout.RIGHT_OF
,它们运行良好,所以这意味着我没有足够的地方来获取按钮?我试图给更多空间,但它仍然不起作用.
I have tried RelativeLayout.BELOW
and RelativeLayout.RIGHT_OF
, and they worked perfectly, so it means I don't have enough place to get the button? I tried to give more space, but it still not work.
我用的是东芝AT100(1280*800)和横向,所以空间够用.测试 below
和 right
与 left
一样.我想如果我在相对布局中放一个控件A,然后我添加控件B并将其声明在控件A的左侧,结果应该是控件B会将控件A推到其右侧,对吗?
I use Toshiba AT100 (1280*800) and landscape, so the space is enough. Test below
and right
just same as the left
. I think If i put an control A in the relativelayout, then I add control B and decalare it's on the left of the control A, the result should be the control B will push the control A to its right, right?
推荐答案
你的假设是错误的,控件 A 不会被推到右边,除非你用 RelativeLayout.LayoutParams
规则指定它.RelativeLayout
如果您没有为它们指定放置规则,则将其子项从屏幕的左上角开始一个一个地放置.当您将 View
A 添加到 RelativeLayout
没有任何规则(如 layout_alignParentRight
)时,它将从左上角开始放置屏幕.然后,当您添加 View
B 时,规则 to_leftOf
将应用于此 View
位置,但此规则对于 View
一个将在屏幕上保持其位置的人.这将使 View
B 位于 View
A 的左侧但在屏幕之外,因为 View
A 边界从 A 的左边界开始屏幕.
Your assumption is incorrect, the control A will not be pushed to the right unless you specified this with a RelativeLayout.LayoutParams
rule. RelativeLayout
places its children one one top of each other starting at the top-left corner of the screen if you don't specify placement rules for them. When you add the View
A to the RelativeLayout
without any rules(like layout_alignParentRight
) it will be placed starting from the top-left corner of the screen. Then, when you add the View
B, the rule to_leftOf
will apply to this View
position but this rule doesn't mean anything for the View
A who will maintain its position on the screen. This will make View
B to be place to the left of View
A but outside of the screen as View
A bounds start from the left border of the screen.
当您使用 layout_alignParentRight="true"
时,Button
将被放置在 ListView
的左侧,因为现在有空间实际看到 Button
(它不在外面了).addView(View child, int index, LayoutParams params)
在 LinearLayout
中起作用,因为 LinearLayout
将其子项排列在一行或一列中(取决于方向)所以当您在特定位置添加 View
时,它会将其后的其他 Views
推到右侧或下方(取决于方向)(没有相对视图在 LinearLayout
中的定位,唯一的规则是孩子一个接一个).
The Button
will be placed to the left of the ListView
when you use layout_alignParentRight="true"
because there is now space to actually see the Button
(it's not outside anymore). addView(View child, int index, LayoutParams params)
works in a LinearLayout
because the LinearLayout
arranges its children in a row or column(depending on orientation) so when you add a View
at a specific position, it will push the other Views
after it to the right or below(depending on orientation)(there is no relative positioning of the views in a LinearLayout
, the only rule is that the children come one after the other).
从没有设置任何规则的 ListView
开始,这里是一个关于如何使 Button
出现在 ListView:
Starting with the
ListView
without any rules set on it, here is an example on how to make the Button
to appear on the left of the ListView
:
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
Button button2 = new Button(this);
button2.setText("I am button 2");
button2.setId(1000);
m_relativeLayout.addView(button2, layoutParams);
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) m_listView
.getLayoutParams();
rlp.addRule(RelativeLayout.RIGHT_OF, button2.getId());
按钮将正常添加到屏幕上,并从屏幕的左上角开始出现.如果没有上面代码中的两行代码,
Button
和 ListView
将重叠,因为这是 RelativeLayout
对孩子的正常行为,对他们没有任何规则.然后我们显式修改 ListView
的位置以将其向右移动(使用上面代码的最后两行).
The Button will be added as normal to the screen and it will appear starting from the top-left corner of the screen. Without the two lines from the code above the
Button
and ListView
will overlap as this is the normal behavior of RelativeLayout
for children without any rules on them. We then explicitly modify the position of the ListView
to move it to the right(with the last two line from the code above).
这篇关于RelativeLayout 添加规则“RelativeLayout.LEFT_OF";不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!