【问题标题】:Removing margin from spinner dropdown从微调器下拉列表中删除边距
【发布时间】:2015-10-13 14:56:26
【问题描述】:

我有一个如下图所示的微调器;

如何消除微调器与其下拉菜单之间左侧 x 的间隙 - 最好是适用于 API8 的解决方案,因为我试图将我的应用程序要求保持在尽可能低的水平。

我以为这将是微调器样式中的 layout_margin,但在阅读 this question 之后,这似乎是不可能的。

在我的主题中我有;

<style name="AppTheme" parent="AppBaseTheme">        
        <item name="android:dropDownListViewStyle">@style/DropDownStyle</item>
    <item name="android:dropDownSelector">@style/DropDownStyle</item>                
</style>


<style name="DropDownTopStyle">

   <item name="android:clickable">true</item>
   <item name="android:background">@drawable/dropdowntop</item>

</style>

<style name="DropDownStyle">

   <item name="android:layout_width">fill_parent</item>
   <item name="android:layout_height">fill_parent</item> 
   <item name="android:layout_marginLeft">0dp</item> 
   <item name="android:layout_margin">0dp</item> 

   <item name="android:clickable">true</item>
   <item name="android:background">@drawable/dropdownback</item>
   <item name="android:popupBackground">@drawable/dropdownback</item>
   <item name="android:cacheColorHint">#FFF000</item>    
  </style>

谢谢, 托马斯

附加; 我可以看到可能有一种方法可以在代码中自己制作一个弹出窗口 - 如果有必要,我可以以某种方式进入适配器弹出视图吗? (这是它显示的列表)。尝试从头开始重新创建整个适配器的行为似乎是一种糟糕的方式 - 但如果我可以进入该视图并禁用正常的弹出行为,那么我可以制作自己的弹出窗口而不会出现烦人的偏移。

【问题讨论】:

  • 你有没有想出解决方案?
  • @AnubianNoob 请将您的布局发布到paste bin
  • 这是一个非常笼统的问题。我有一个自定义微调器,在附加到服务的浮动窗口中有一个下拉菜单。我的布局真的很重要吗?
  • @AnubianNoob 这不是一个普遍的问题,因为我似乎无法重现这个问题。

标签: android android-layout spinner android-spinner


【解决方案1】:

似乎您需要扩展 Spinner View 以制作您自己的显示项目列表的 AlertDialog,或者在较新版本(API 16+)中您可以使用

android:dropDownHorizontalOffset="-8dp"

在此处查看完整的详细信息:How to change the position of opened spinner?

【讨论】:

    【解决方案2】:

    你想要这样的东西吗?

    如果是这样,这里是代码(直到 API 级别 9):

    活动:

    public class MainActivity extends Activity
    {
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Spinner spinnerListOptions = (Spinner) findViewById(R.id.spinner);
            ArrayAdapter<CharSequence> adapterListOptions = ArrayAdapter.createFromResource(this, R.array.string_array,
                R.layout.spinner_item_top);
            adapterListOptions.setDropDownViewResource(R.layout.spinner_item_dropdown);
            spinnerListOptions.setAdapter(adapterListOptions);
        }
    }
    

    spinner_item_top.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="wrap_content"
          android:layout_height="29dp"
          android:textSize="12sp"
          android:gravity="center">
    </TextView>
    

    spinner_item_dropdown.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="wrap_content"
          android:layout_height="29dp"
          android:textSize="12sp"
          android:gravity="center">
    </TextView>
    

    activity_main.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
       <Spinner
               android:id="@+id/spinner"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_centerInParent="true"/>
    
    </RelativeLayout>
    

    strings.xml:

    <resources>
    <string-array name="string_array">
        <item>item 1</item>
        <item>item 2</item>
        <item>item 3</item>
    </string-array>
    </resources>
    

    【讨论】:

      【解决方案3】:

      试试这个,应该可以的

          <style name="DropDownStyle">
      
         <item name="android:layout_width">0dp</item>
      

      【讨论】:

        【解决方案4】:

        我不t know it is possible, because all view item like Spinner, EditText, etc... are small images in the android SDK. You should check, is there any default padding in the 9patched SDK image, if yes, I think you cant 这样做。 另外,不要忘记检查 Android 源代码中的基本 Spinner 类,也许有一些技巧。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-09-26
          • 1970-01-01
          • 1970-01-01
          • 2015-06-19
          • 2021-12-17
          • 1970-01-01
          • 2015-03-30
          • 1970-01-01
          相关资源
          最近更新 更多