android Popupwidnow+spinner及spinner无法显示文字

相信大家对Spinner这个控件都不陌生,它就是一个下拉列表选择控件,今天在项目中用到这个控件的时候遇到了两个问题,于是我在这里写下来,

1.Popupwindow+Spinner结合使用的时候spinner必须要设置成dialog模式,即

android:spinnerMode="dialog"

至于其中原因我也大致查了一下,spinnerMode两种模式分别:dialog,dropdown,其中dialog就是我们平常用的对话框Dialog,而dropdown对应的是一个Popupwindow。那么这意味着什么呐?如果spinnerMode设置为dialog就相当于Popupwindow+dialog,如果spinnerMode设置为dropdown就相当于Popupwindow+Popupwdindow了,这也是为什么当我们在Popupwindow+spinner时候spinnerMode只能设置为dialog

2.Spinner列表有数据但是不显示文字(这里指:自定义布局设置适配器),意思就是Spinner控件不显示文字包括默认和选中列表文字.但是列表又有数据,于是我就代码注释,直接在xml里面添加

android:entries="@array/area_type"

运行编译后发现列表与文字都正常.

更加奇怪的事情是:用系统布局给spinner设置适配器的时候一切都是正常的,遇到这种问题,我有点懵逼咯!

贴上布局中Spinner部分Xml

<android.support.constraint.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@drawable/edit_background"
        app:layout_constraintBottom_toBottomOf="@id/popuwindow_laiyuan"
        app:layout_constraintHorizontal_bias="0.17"
        app:layout_constraintHeight_percent="0.07"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@id/popuwindow_laiyuan"
        app:layout_constraintWidth_percent="0.165">

    <android.support.v7.widget.AppCompatSpinner
            android:id="@+id/popupwindow_khly"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:entries="@array/area_type"
            android:saveEnabled="true"
            android:spinnerMode="dialog"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

分析几个点:1.有列表表明线程及数据源没问题且适配器设置也没问题

                   2.用系统布局设置适配器就正常显示,代表不是嵌套及外在因素.

                   3.用静态数据在xml设置都正常显示

                   4.由3可得我spinner控件本身布局及其他设置没问题

综合上诉4个条件,我只有再次打开了我自定义布局的item文件了。

 

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <TextView
            android:id="@+id/spinner_item"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="10dp"
            android:text="xxxx"
            android:textColor="@color/bottom"
            android:textSize="16sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

没错,item文件就一个textview,这么一看还真看不出什么问题来,我也在网上看了不少文章,都是两个点:spinnerMode和什么padding设置,首先我没考虑spinner布局本身问题,此外这个item布局的padding我设置的10dp,难道这个跟spinner不显示文章有什么关系,尼玛反正找不到其他办法,先试试于是我就把item布局中 android:padding="10dp"去掉了,运行,编译....意外出现了居然这时候显示出文字了,这时候我就想到会不会这个spinner显示的文字其实就是item布局的textview,于是我又改了下item布局中textview的padding值,看看spinner文字会不会移动,果然是移动了。这也就解决了我的问题了...

那么我总结下这个问题的原因:其实一般情况我们设置Spinner宽高自适应基本上不会遇到这个问题,我这里给spinner设置了一个百分比宽高即固定宽高了,那么此时设置padding即有可能导致文字显示不出来.

欢迎各位android,flutter爱好者加群交流:290611780

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值