2011-12-29 142 views
3

当我将两个纺纱器放在海绵旁时,我遇到了这个问题。这里是布局的XML片段:彼此相邻的两个纺纱器

... 
<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:weightSum="2" > 

    <Spinner 
     android:id="@+id/x" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="top|left" 
     android:layout_weight="1" /> 

    <Spinner 
     android:id="@+id/y" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="top|right" 
     android:layout_weight="1" /> 
</LinearLayout> 

<RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> 

    <ListView 
     android:id="@+id/z" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:cacheColorHint="#FFFFFF" > 
    </ListView> 
... 
... 

下面是结果:

spinners

我尝试了许多不同的事情。我试着改变重量,重力,将父母改为RelativeLayout,但结果保持不变。

请帮忙!

编辑:

好的。我知道了。一些冗余,但它解决了这个问题。有点奇怪,为什么这个工作和“正常的方式”没有。感谢大家的帮助。

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:weightSum="2" > 

    <Spinner 
     android:id="@+id/x" 
     android:layout_width="0dip" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 

    <RelativeLayout 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" > 

     <Spinner 
      android:id="@+id/y" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" /> 
    </RelativeLayout> 
</LinearLayout> 
+0

我无法重现您遇到的问题。你是否试过剥离除了两个相邻的spinners之外的所有东西 - 所以只有一个LinearLayout作为root和两个spinners?另外,您是否在Eclipse的预览版中以及实际的设备/仿真器中遇到了这个问题? – 2011-12-29 19:52:03

回答

0

我从来没有看到确切的问题,但我看到你既填补了家长及其宽度,这可能会发生冲突。我会用android:layout_width="0dip"替换所有使用重量的元素来确定它的大小,这样他们实际上是相等的。

+0

我刚换了它。没有帮助不幸。让我补充一点,这个问题也出现在风景中。 – Paul 2011-12-29 18:50:59

+0

很抱歉听到这个问题,我提出了这个问题,希望它能得到回复。 – Pyrodante 2011-12-29 18:53:03

+0

谢谢。我也这样希望。 – Paul 2011-12-29 19:26:58

1

在RelativeLayout的,你可以使用android:layout_toRightOf="@+id/x"android:layout_alignTop="@+id/x"

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout> 
    <Spinner 
     android:id="@+id/x" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 
    <Spinner 
     android:id="@+id/y" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@+id/x" 
     android:layout_alignTop="@+id/x" /> 
</RelativeLayout> 

(我已经离开了重力定义,因为他们似乎并没有对您的微调文字的效果。)

+0

试过了。完全使用您给出的解决方案产生以下结果:第一个(最左边的)微调器填充父项,将另一个推离屏幕。此外,切换到横向会导致发生异常。例外说android不能找到“y”(“y”是第二个(最右边的))微调。 – Paul 2011-12-29 19:31:50

+0

我试着为第一个微调器设置一个固定的宽度。这工作。但是现在我没有让他们每人填充父母宽度的50%。此外,我上面提到的例外仍然存在。 – Paul 2011-12-29 19:35:00

-1

虽然我不能看到这段代码有什么问题,但我认为你不需要设置引力,因为wrap_content高度会自动将顶点和左右的线性布局放在顶部,左右不是这种情况,因为两个spinners都是屏幕的一半宽度。

所以要用:

<Spinner 
    android:id="@+id/x" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" /> 

<Spinner 
    android:id="@+id/y" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" /> 

+0

不是。同样的问题。不过谢谢。 – Paul 2011-12-29 19:28:30

5

也能正常工作对我来说:

<LinearLayout 
    android:id="@+id/x" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:weightSum="2"> 
    <Spinner 
     android:id="@+id/s1" 
     android:layout_width="0dip" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 
    <Spinner 
     android:id="@+id/s2" 
     android:layout_width="0dip" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 
</LinearLayout> 
0

替代解决方案,它为我工作。我添加了android:layout_gravity =“center_vertical”到微调如下:

<LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center_vertical" 
        android:layout_marginTop="10dp" 
        android:orientation="horizontal" > 

        <Spinner 
         android:id="@+id/custSpinner" 
         style="@android:style/Widget.Spinner" 
         android:layout_width="0dp" 
         android:layout_height="wrap_content" 
         android:layout_gravity="center_vertical" 
         android:layout_weight="1" 
         /> 

        <Spinner 
         android:id="@+id/busSpinner" 
         style="@android:style/Widget.Spinner" 
         android:layout_width="0dp" 
         android:layout_height="wrap_content" 
         android:layout_gravity="center_vertical" 
         android:layout_weight="1" 
         /> 
       </LinearLayout>