Android showAsDropDown的诡异显示

来源:互联网 发布:淘宝网卖家中心官网 编辑:程序博客网 时间:2024/06/05 12:41

我们都知道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时遇到的比较怪异的显示。

阅读全文
2 0
原创粉丝点击