2011-05-02 88 views
1

我的屏幕上有一些弹出窗口,需要的东西不那么常见。查看另一个视图

我将带有Header + Content + Footer的弹出窗口布局为LinearLayout。但我需要一个小箭头才能显示在我的组件上。

当弹出窗口位于锚点上方并且箭头向下时,我使用下面的代码来绘制它。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/content" android:orientation="vertical" 
android:layout_width="wrap_content" android:layout_height="wrap_content"> 

<LinearLayout android:id="@+id/header" android:background="@drawable/background_header" 
    android:orientation="horizontal" android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
</LinearLayout> 

<HorizontalScrollView android:background="@drawable/background" 
    android:layout_width="wrap_content" android:layout_height="match_parent" 
    android:scrollbars="none"> 

</HorizontalScrollView> 

<ImageView android:id="@+id/arrow_down" android:layout_width="wrap_content" 
    android:layout_height="wrap_content" android:layout_marginTop="-3dip" 
    android:src="@drawable/quickcontact_arrow_down" /> 
</LinearLayout> 

在运行时,我能够把箭头正好与下面的代码锚以上。

ViewGroup.MarginLayoutParams param = (ViewGroup.MarginLayoutParams) mArrowDown 
      .getLayoutParams(); 
    param.leftMargin = root.getMeasuredWidth() 
      - (screenWidth - anchor.getLeft()); 

它显示正确。

现在我需要做同样的事情,但箭头需要显示在上方。 我的问题是箭头需要在另一个视图上重叠一点(导致背景色匹配它们),所以这就是为什么它需要在后面绘制。

我试着用FrameLayout让“内容”有一些topMargin,但它不工作。

我知道这可以用AbsoluteLayout来完成,但我不惜一切代价避免它。

编辑:

以下Josh答案,我写了下面的代码。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" android:layout_height="wrap_content"> 
<LinearLayout android:id="@+id/content" 
    android:orientation="vertical" android:layout_width="wrap_content" 
    android:layout_height="wrap_content" android:fadingEdgeLength="0dip"> 

    <RelativeLayout android:id="@+id/header" 
     android:background="@drawable/background_header" android:orientation="horizontal" 
     android:layout_width="match_parent" android:layout_height="wrap_content"> 
    </RelativeLayout> 

    <HorizontalScrollView android:background="@drawable/background" 
     android:layout_width="wrap_content" android:layout_height="wrap_content" 
     android:scrollbars="none"> 
    </HorizontalScrollView> 
</LinearLayout> 
<ImageView android:id="@+id/arrow_up" android:layout_above="@id/content" 
    android:layout_width="wrap_content" android:layout_height="wrap_content" 
    android:src="@drawable/quickcontact_arrow_up" /> 
</RelativeLayout> 

但我不知道为什么,箭头现在不显示。

回答

4

我不会假装读取了所有的xml。不过,我认为你想要的是RelativeLayout。您应该可以使用这种技术将任何小箭头放在任何你喜欢的地方,相对于包含它的RelativeLayout的边界。

例如,如果您将所有内容都包含在单个RelativeLayout中,然后添加一个Button作为第二个项目,则可以为按钮属性(如alignParentRight = true和layout_marginRight = 10dp)放置按钮10dp从屏幕的右边缘开始,在任何视图的ON TOP上都已经存在。

+0

我知道,这是一个很强烈的代码。我要测试你的解决方案。 – 2011-05-02 15:40:47

+0

指出@Josh,我同意你的看法,'RelativeLayout'肯定能解决问题。 +1为紧凑desc。的解决方案。 – rekaszeru 2011-05-02 15:47:35

+0

好吧,我试着按照你的解决方案,但箭头现在不出现。我添加了代码,我在我的问题上尝试。 – 2011-05-02 15:57:02

相关问题