Android 相对布局添加规则“RelativeLayout.LEFT_OF”不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10700273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:40:04  来源:igfitidea点击:

RelativeLayout add rule "RelativeLayout.LEFT_OF" not working

androidandroid-layoutandroid-relativelayout

提问by dreamtale

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:

在java代码中,我想在listview的左侧添加一个视图,但是没有用:

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?

只有当我将列表视图设置为alignParentRight 时,它才会起作用。这是一个android错误还是我遗漏了什么?

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_OFwork?

我总是尝试addView(View child, int index, LayoutParams params),但它可能只适用于线性布局。那么有没有正常的解决方案来完成这项RelativeLayout.LEFT_OF工作?

EDIT

编辑

I have tried RelativeLayout.BELOWand 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.

我试过RelativeLayout.BELOWRelativeLayout.RIGHT_OF,它们工作得很好,所以这意味着我没有足够的地方来获得按钮?我试图给更多的空间,但它仍然不起作用。

I use Toshiba AT100 (1280*800) and landscape, so the space is enough. Test belowand rightjust 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?

我用的是东芝AT100(1280*800)和横向,所以空间够用。测试belowright一样left。我想如果我将控件 A 放在相对布局中,然后我添加控件 B 并将其声明在控件 A 的左侧,结果应该是控件 B 会将控件 A 推到其右侧,对吗?

回答by Luksprog

I think If i put an control A in the relativelayout, then i add control B and declare 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,然后我添加控件 B 并声明它在控件 A 的左侧,结果应该是控件 B 将控件 A 推到其右侧,对吗?

Your assumption is incorrect, the control A will not be pushed to the right unless you specified this with a RelativeLayout.LayoutParamsrule. RelativeLayoutplaces 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 ViewA to the RelativeLayoutwithout any rules(like layout_alignParentRight) it will be placed starting from the top-left corner of the screen. Then, when you add the ViewB, the rule to_leftOfwill apply to this Viewposition but this rule doesn't mean anything for the ViewA who will maintain its position on the screen. This will make ViewB to be place to the left of ViewA but outside of the screen as ViewA bounds start from the left border of the screen.

你的假设是错误的,除非你用RelativeLayout.LayoutParams规则指定,否则控件 A 不会被推到右边。RelativeLayout如果您没有为它们指定放置规则,则将其子项从屏幕的左上角开始一个一个地放置。当您将ViewA添加到RelativeLayout没有任何规则(如layout_alignParentRight)时,它将从屏幕的左上角开始放置。然后,当您添加ViewB 时,该规则to_leftOf将适用于该View位置,但该规则对于View将在屏幕上保持其位置的A没有任何意义。这将使ViewB 位于ViewA的左侧但在屏幕之外,因为ViewA 边界从屏幕的左边界开始。

The Buttonwill be placed to the left of the ListViewwhen 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 LinearLayoutbecause the LinearLayoutarranges its children in a row or column(depending on orientation) so when you add a Viewat a specific position, it will push the other Viewsafter 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).

Button将被放置到的左侧ListView,当你使用layout_alignParentRight="true",因为现在有空间,实际看到的Button(它不是外面了)。addView(View child, int index, LayoutParams params)在 a 中工作是LinearLayout因为将LinearLayout其子项排列在一行或一列中(取决于方向),因此当您View在特定位置添加 a时,它会将Views其后的另一个推到右侧或下方(取决于方向)(没有相对视图在 a 中的定位LinearLayout,唯一的规则是孩子们一个接一个)。

Starting with the ListViewwithout any rules set on it, here is an example on how to make the Buttonto appear on the left of the ListView:

ListView没有设置任何规则开始,这里是一个关于如何使Button出现在 左侧的示例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 Buttonand ListViewwill overlap as this is the normal behavior of RelativeLayoutfor children without any rules on them. We then explicitly modify the position of the ListViewto move it to the right(with the last two line from the code above).

按钮将正常添加到屏幕上,并从屏幕的左上角开始出现。如果没有上面代码中的两行,Button并且ListView将重叠,因为这是RelativeLayout儿童的正常行为,对他们没有任何规则。然后我们显式修改 的位置ListView以将其向右移动(使用上面代码的最后两行)。

回答by Barak

If your variable names are indicative, it's because you are adding the widget to a LinearLayout, so tags for a RelativeLayout get ignored.

如果您的变量名称是指示性的,那是因为您将小部件添加到 LinearLayout,所以相对布局的标签会被忽略。

This line is the one I'm talking about:

这一行是我正在谈论的那一行:

m_linearLayout.addView(button2, layoutParams);

EDIT

编辑

You say alignParentRightworks... the only difference there is that ot doesn't take an anchor parameter. Perhaps m_listView.getId()isn't returning the proper id. You could step through with the debugger and see if it's returning a proper value.

你说alignParentRight有效......唯一的区别是 ot 不采用锚参数。也许m_listView.getId()没有返回正确的 id。您可以逐步调试调试器,看看它是否返回了正确的值。

Maybe you could try calling the id specifically...

也许你可以尝试专门调用id...

layoutParams.addRule(RelativeLayout.LEFT_OF, R.id.list);

回答by Manoj Singh Rawal

To perform it, use predefined view IDor declare one. In values foldercreate ids.xmlthen add a Itemlike this:

要执行它,请使用预定义的视图ID或声明一个。在values 文件夹中创建ids.xml然后添加一个这样的项目

<item name="imageViewID" type="id"/>

use this id in your code where you are creating new Instance of view like this:

在您创建新视图实例的代码中使用此 ID,如下所示:

RelativeLayout layout=new RelativeLayout(context);

ImageView imageView = new ImageView(context);
imageView.setId(R.id.imageViewID);

RelativeLayout.LayoutParams layoutParams = new LayoutParams(50, 50);
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
layout.addView(imageView, layoutParams);

TextView  textView = new TextView(context);

RelativeLayout.LayoutParams textViewParams= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
textViewParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
textViewParams.addRule(RelativeLayout.BELOW, imageView.getId());
layout.addView(nameView, nameLayoutParams);

or we can directly use this function View.generateViewId()to perform the same. Like this:

或者我们可以直接使用这个函数View.generateViewId()来执行相同的操作。像这样:

imageView.setId(View.generateViewId());

回答by bhavindesai

I think you might have forgotten to add m_listViewto the RelativeLayoutor m_listView's visibility would be GONE.

我想你可能会忘记添加m_listViewRelativeLayoutm_listView的知名度会GONE

Can you please check for that?

你能检查一下吗?

回答by user3093743

setId before align is called, especially for the new object view.

调用 align 之前的 setId,特别是对于新的对象视图。

回答by PatrickNLT

If you are using a custom id and not a regular generated Android id (eg. R.id.my_id), make sure that the id is not equal to 0 (or negative), otherwise the rule will be ignored.

如果您使用的是自定义 id 而不是常规生成的 Android id(例如R.id.my_id),请确保该 id 不等于 0(或负数),否则该规则将被忽略。