Android showAsDropDown的诡异显示

本文详细探讨了在特定布局条件下,PopupWindow使用showAsDropDown方法显示位置的变化规律,并总结了其显示位置判断准则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们都知道showAsDropDown(View anchor, int xoff, int yoff)的用法,这是PopupWindow的一种显示方式,表示显示在anchor的正左下方,xoff与off表示x轴与y轴的偏移量。一般显示类似于微信



正常来说这样显示是没有问题的,不过今天我遇到了一个怪现象,它显示在了anchor的正左上方,特此来记录一下!


首先看我的布局文件:

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="200dp" />

    <LinearLayout
        android:id="@+id/ll_bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#000"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_dir_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="10dp"
            android:textColor="#fff"
            tools:text="sd/0/images" />

        <TextView
            android:id="@+id/tv_dir_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:textColor="#fff"
            tools:text="5张" />
    </LinearLayout>
</LinearLayout>

显示如下:


在activity中的代码如下:

dirListPopWindow = new PhotoDirListPopWindow(this,
                LayoutInflater.from(this).inflate(R.layout.layout_list_dir, null),
                ViewGroup.LayoutParams.MATCH_PARENT, (int) (mScreenHeight * 1.3),
                mFolders
        );
dirListPopWindow.showAsDropDown(llBottom, 0, 0);

显示结果如下:




现在改变一下布局文件:

<android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="280dp" />

仅改变了它的高度,其他的没变,显示结果如下:


结果可以看到,明明调用的是showAsDropDown方法,结果显示在了正左上方,通过反复的几次验证,得出一下结论:

当调用showAsDropDown(View anchor, int xoff, int off)时,anchor控件所在屏幕下方的高度记为mBottomHeight,

它距离上方的位置记录为mTopHeight,PopupWindow的高度记为mHeight

1,当mBottomHeight > mHeight时,显示在正

2,当mBottomHeight < mHeight时,但是mBottomHeight > mTopHeight时,显示在正

3,当mBottomHeight < mHeight时,但是mBottomHeight < mTopHeight时,显示在正


以上是使用showAsDropDown时遇到的比较怪异的显示。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值