【问题标题】:How do I place spinner below the spinner layout when it is clicked and show spinner item in lollipop and above?单击时如何将微调器放置在微调器布局下方并在棒棒糖及上方显示微调器项目?
【发布时间】:2015-11-03 05:18:46
【问题描述】:

就像溢出菜单与工具栏重叠一样,当它在棒棒糖及更高版本的 android 版本中显示下拉项目时,微调器会重叠自身。所以我需要把它放在微调器视图下面而不是它上面。

【问题讨论】:

    标签: android android-layout android-5.0-lollipop android-spinner


    【解决方案1】:

    你可以使用

     android:overlapAnchor="false"
    

    这将显示微调器视图下方的下拉列表(在 api 级别 21 及更高级别上工作)。

    【讨论】:

      【解决方案2】:

      供所有api使用

       android:dropDownVerticalOffset="35dp"
      

      或任何适合您需要的值。

      【讨论】:

        【解决方案3】:

        最近,我遇到了同样的问题,但我在整个应用程序中有几个微调器,我希望它看起来一样,而不必在所有它们中添加相同的属性,所以我使用 style.xml 将我的微调器自定义为下面

        <resources>
        
        <!-- Base application theme. -->
        <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
            <item name="android:spinnerStyle">@style/spinner_style</item>
        </style>
        
        <style name="spinner_style" parent="Widget.AppCompat.Spinner">
            <item name="android:dropDownVerticalOffset">40dip</item>
            <item name="overlapAnchor">false</item>
            <!--Other customizations-->
        </style>
        
        
        </resources>
        

        【讨论】:

          【解决方案4】:

          android:spinnerMode="dropdown" android:dropDownVerticalOffset="50dp"

          这将在微调器视图下方显示下拉菜单。

          【讨论】:

            【解决方案5】:

            在 Spinner 中使用以下属性

            android:dropDownVerticalOffset="35dp"
            

            下面是 Spinner 的代码

                <RelativeLayout
                    android:layout_width="200dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="45dp"
                    android:layout_marginRight="3dp"
                    android:layout_weight=".28"
                    android:orientation="horizontal">
            
                    <Spinner
                        android:id="@+id/spinner_users"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:layout_gravity="left"
                        android:layout_marginLeft="5dp"
                        android:background="@android:color/transparent"
                        android:dropDownVerticalOffset="35dp"
                        android:spinnerMode="dropdown" />
            
                    <ImageView
                        android:layout_width="30dp"
                        android:layout_height="50dp"
                        android:layout_alignParentRight="true"
                        android:layout_centerVertical="true"
                        android:layout_gravity="center"
                        android:src="@drawable/drop_down" />
            
                </RelativeLayout>
            

            【讨论】:

              【解决方案6】:

              这取决于您的微调器所在的位置以及下拉列表中有多少项目。

              如果微调器布局在屏幕顶部,那么android:overlapAnchor="false" 就足够了;

              如果微调器布局围绕屏幕中心有很多项目,例如60 项,然后android:overlapAnchor="false" 不满足您的要求,因为下拉列表没有足够的空间;当微调器位于底部时,情况类似,但微调器上方有下拉列表。

              我认为 Spinner 不是一个灵活的 android 小部件,因为可用的设置不多,例如下拉高度,加载数据时将选择第一项。为了达到同样的目的,我认为ListPopupWindow 是一个很好的替代品。这是一个简单的例子:

              布局:

              <?xml version="1.0" encoding="utf-8"?>
              <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
              android:layout_height="match_parent" android:orientation="vertical" android:gravity="center">
              <TextView
                  android:gravity="center_vertical"
                  android:text="Tap for more options"
                  android:id="@+id/tv_anchor"
                  android:layout_margin="20dp"
                  android:background="@drawable/spinner_bg"
                  android:layout_width="match_parent"
                  android:layout_height="40dp"/>
              </LinearLayout>
              

              活动:

              override fun onCreate(savedInstanceState: Bundle?) {
                  super.onCreate(savedInstanceState)
                  setContentView(R.layout.act_drop_down)
              
                  tvAnchor = findViewById(R.id.tv_anchor)
              
                  val popupList = ListPopupWindow(this)
                  popupList.setAdapter(ArrayAdapter(this, android.R.layout.simple_spinner_item, getData()))
                  popupList.anchorView = tvAnchor
                  popupList.width = AbsListView.LayoutParams.WRAP_CONTENT
                  popupList.height = 100*3
              
                  popupList.setOnItemClickListener { parent, view, position, id ->
                      popupList.dismiss()
                  }
              
              
                  tvAnchor.setOnClickListener {
                      popupList.show()
                  }
              }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2023-03-09
                • 1970-01-01
                • 2017-05-27
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多