我的屏幕上有一些弹出窗口,需要一些不太常见的东西。

我将带有标题+内容+页脚的弹出窗口布局到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并让“ content”具有一些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>


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

最佳答案

我不会假装已经阅读了所有的xml。我认为您想要的是RelativeLayout。相对于包含它的RelativeLayout的边界,您应该能够使用此技术将任何小箭头视图放置在任意位置。

例如,如果将所有内容包装在单个RelativeLayout中,然后添加一个Button作为第二项,则可以给buttonParentRight = true和layout_marginRight = 10dp这样的按钮属性,以将按钮10dp从右边缘放置屏幕顶部的任何视图都位于顶部。

07-24 09:47
查看更多