2012-05-22 37 views
14

我有一个RelativeLayout的象下面这样:的RelativeLayout添加规则“RelativeLayout.LEFT_OF”不工作

<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代码中,我想一个视图添加到列表视图的左侧,但是事实并非如此工作:

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 bug或我错过了什么?

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

编辑

我试图RelativeLayout.BELOWRelativeLayout.RIGHT_OF,并且他们的工作完美,所以这意味着我没有足够的地方,以获得该按钮?我试图给更多的空间,但它仍然无法工作。

我使用东芝AT100(1280 * 800)和风景,所以空间够了。测试belowrightleft相同。我想如果我把控件A放在相关布局中,那么我将控件B和控件A添加到控件A的左边,结果应该是控件B将控件A推向右边,对吧?

+0

视图在哪里出现? – Barak

+0

relativelayout是活动的主视图,它具有添加按钮的空间。 – dreamtale

+0

我的意思是,按钮是不是出现,或出现在不适当的地方? – Barak

回答

22

我想如果我把一个控制一个在RelativeLayout的,然后我加控制B和声明是在控制左侧A,结果应该是控制B会将控制A推向右边,对吧?

你的假设是不正确,控制用A不会,除非你指定该用RelativeLayout.LayoutParams规则推向右侧。 RelativeLayout如果您没有为它们指定展示位置规则,则从屏幕的左上角开始将其子项放在彼此的顶部。当您将View A添加到RelativeLayout而没有任何规则(如layout_alignParentRight)时,它将从屏幕的左上角开始放置。然后,当您添加View B时,规则to_leftOf将适用于此位置View,但此规则并不意味着将保持其在屏幕上的位置的任何内容。这将使View B位于View A的左侧,但在屏幕的外侧作为View界限从屏幕的左边界开始。当您使用layout_alignParentRight="true"

Button将被放置到ListView左边因为现在还有空间,真正看到Button(它不是外面了)。 addView(View child, int index, LayoutParams params)LinearLayout中工作,因为LinearLayout由于LinearLayout将其子项排列成一行或一列(取决于方向),所以当您在特定位置添加View时,它会将其他Views推到右侧或下侧(取决于方向) (在LinearLayout中没有相对的意见定位,唯一的规则是孩子一个接一个地来)。

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()); 

该按钮将被添加为正常的屏幕上,它会从屏幕的左上角开始显示。如果没有上述代码中的两行,ButtonListView将重叠,因为这是RelativeLayout对于没有任何规则的孩子的正常行为。然后,我们明确修改ListView的位置以将其移到右侧(以上代码的最后两行)。

+0

我使用东芝AT100(1280 * 800)和风景,所以空间足够测试''下''和'右''就像'左'一样。我想如果我把一个控件A放在相关布局中,那么我在控件A的左边添加控件B和decalare,结果应该是控件B将控件A推到它的右边,对吧? – dreamtale

+0

@dreamtale我第一次误解了你的问题。我编辑了我的答案,看看是否有帮助。 – Luksprog

+0

感谢您的精心解答,我已使其工作:) – dreamtale

1

我想你可能忘记了将m_listView加到RelativeLayoutm_listView的可见度应该是GONE

你能检查一下吗?

+0

XML显示布局。 – dreamtale

+0

正如我所看到的,您正在设置相对布局的布局参数,但您在线性布局中添加按钮。 Eiter使用LinearLayout.LayoutParams而不是RelativeLayout。LayoutParams或调用RelativeLayout实例的addView方法。 – bhavindesai

+0

看到我编辑的问题。 – dreamtale

2

如果您的变量名称是指示性的,那是因为您要将该小部件添加到LinearLayout中,因此将忽略RelativeLayout的标记。

这行是一个我在谈论:

m_linearLayout.addView(button2, layoutParams); 

编辑

你说alignParentRight作品...唯一的区别还有就是OT不走锚参数。也许m_listView.getId()没有返回正确的ID。你可以通过调试器来看看它是否返回一个合适的值。

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

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

你的意思是我应该使用linearlayout?你能给我更多的细节吗? – dreamtale

+0

不,我的意思是你需要添加一个视图到RelativeLayout以使RelativeLayout规则起作用。该视图引用说'm_linearlayout',它对我说,你正试图添加视图到一个LinearLayout而不是一个RelativeLayout。由于您未显示设置'm_linearlayout'参考的位置,因此我假定您使用的是适当的名称。除非你在某处有'm_linearlayout =(RelativeLayout)this.findViewById(R.id.parent)',在这种情况下,我会说你已经为你的相对布局选择了一个很差的参考名称。 – Barak

+0

对不起,我在项目前试过了,只是将'LinearLayout'替换为'RelativeLayout',不要更改变量名称。我编辑了这个问题。 – dreamtale

1

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

0

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

2

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

<item name="imageViewID" type="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); 

或我们可以直接使用此功能View.generateViewId()执行相同的操作。像这样:

imageView.setId(View.generateViewId());