问题描述
我有一个RelativeLayout的象下面这样:
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android 机器人:layout_width =FILL_PARENT 机器人:layout_height =FILL_PARENT 机器人:方向=横向 机器人:ID =@ + ID /父> <的ListView 机器人:layout_width =360dp 机器人:layout_height =600dp 机器人:ID =@ + ID /列表 机器人:inputType =文本 机器人:MAXLINES =1 机器人:layout_margin =50dp 机器人:layout_alignParentRight =真 /> < / RelativeLayout的>
在Java的code,我想一个视图添加到列表视图的左边,但它并没有奏效:
m_relativeLayout =(RelativeLayout的)findViewById(R.id.parent); RelativeLayout.LayoutParams的LayoutParams =新RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); layoutParams.addRule(RelativeLayout.LEFT_OF,m_listView.getId()); 按钮按钮2 =新的按钮(这一点); button2.setText(我是按钮2); m_relativeLayout.addView(按钮2,的LayoutParams);
只有当我设置列表视图为 alignParentRight ,它会奏效。这是一款Android的bug还是我失去了一些东西?
我总是试图 addView(查看孩子,INT指数的LayoutParams PARAMS),但它可能只工作在LinearLayout中。那么,有没有一种正常的解决方案,使 RelativeLayout.LEFT_OF 工作?
修改
我已经试过 RelativeLayout.BELOW 和 RelativeLayout.RIGHT_OF ,和他们完美地工作,所以这意味着我不没有足够的地方,以获得按钮?我试着给更多的空间,但它仍然无法正常工作。
我用东芝AT100(1280 * 800)和景观,所以空间就足够了。测试下面和右键只是相同离开。我想,如果我把一个控制一个在RelativeLayout的,然后我加控制B和decalare它在控制A的左侧,其结果应该是控制B就按下控制A到它的权利,对吧?
您的假设是不正确的,除非你有一个 RelativeLayout.LayoutParams 规则中指定该控件A将不会被推向右侧。 RelativeLayout的将它的孩子们彼此开始在屏幕的左上角的一个一个顶部,如果你不为其指定放置规则。当您添加查看 A到 RelativeLayout的无任何规则(如 layout_alignParentRight )将被放置在屏幕的左上角开始。然后,当您添加查看 B,规则 to_leftOf 将适用于本查看的位置,但这个规则没有任何意义的查看 A谁将会保持其在屏幕上的位置。这将使查看 B是发生在屏幕的查看 A左侧,但外面查看 A的境界,从屏幕左侧边缘开始。
在按钮将被放置到的ListView 当你使用 layout_alignParentRight左=真正的,因为现在有空间,实际看到按钮(这不是外面了)。 addView(查看孩子,INT指数的LayoutParams PARAMS)工作在一个的LinearLayout ,因为的LinearLayout 排在一行或一列其子(取决于方向),所以当你在一个特定的位置增加查看,这将推动其他浏览后,向右或以下(取决于方向)(存在的观点没有相对定位一个的LinearLayout ,唯一的规则是,孩子们来了一前一后)。
与启动的ListView ,而不在其上设置任何规则,这里是如何使按钮为例出现在 ListView控件的左:
RelativeLayout.LayoutParams的LayoutParams =新RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 按钮按钮2 =新的按钮(这一点); button2.setText(我是按钮2); button2.setId(1000); m_relativeLayout.addView(按钮2,的LayoutParams); RelativeLayout.LayoutParams RLP =(RelativeLayout.LayoutParams)m_listView .getLayoutParams(); rlp.addRule(RelativeLayout.RIGHT_OF,button2.getId());
的按钮将被添加为垂直于屏幕,它就会出现从屏幕的左上角开始。如果没有从code以上的按钮两行和的ListView 将重叠,因为这是正常的行为RelativeLayout的 <$ C C $>儿童不会对他们的任何规则。然后,我们明确地修改的ListView 将其移动到右侧(与最后两行从code以上)的位置。
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>
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);
only if I set the listview to alignParentRight, it will work. Is this an android bug or I'm missing something?
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?
EDIT
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.
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?
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.
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).
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());
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的添加规则&QUOT; RelativeLayout.LEFT_OF&QUOT;不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!