【问题标题】:Android: Custom spinner size problemsAndroid:自定义微调器大小问题
【发布时间】:2011-10-24 01:56:10
【问题描述】:

我制作了一个自定义微调器,但尺寸并不是我想要的。微调器变得非常大,以获得我需要的列表中的间距。我希望能够独立于微调器按钮的大小来调整微调器的行数。我希望微调器变薄,然后我希望部分行间隔很大。 (见底部图片):

目前微调器的行的 xml 是这样的:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:padding="5dip">
        <ImageView android:layout_width="32sp" android:src="@drawable/icon"
            android:id="@+id/spinnerimage" android:layout_height="32sp" />
        <TextView android:textSize="22sp" android:textStyle="bold" android:textColor="#000"
            android:layout_width="fill_parent" android:id="@+id/category"
            android:layout_height="fill_parent" android:paddingLeft="5sp" />
    </TableRow>
</TableLayout>

我想使用相对布局,但表格布局给了我更好的间距。如果我尝试制作高度,它将切断行中的文本和图标。我的 main.xml 中的微调器是:

        <Spinner android:id="@+id/catspinner"
        android:layout_marginLeft="25dip" android:layout_marginRight="25dip"
        android:layout_width="fill_parent" android:layout_centerHorizontal="true"
        android:layout_height="wrap_content" android:prompt="@string/prompt"
        android:background="@drawable/yellow_btn"
        android:layout_centerVertical="true" android:drawSelectorOnTop="true" />

我希望微调器的尺寸与标准的 android 微调器相同(右侧)。我的当前已反转,微调器太大而行间距太小。

有什么想法吗??

【问题讨论】:

    标签: android xml layout spinner android-custom-view


    【解决方案1】:

    在设置适配器时说adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 这应该工作......

    【讨论】:

      【解决方案2】:

      根据您的需要使用以下代码...

      ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.Questions, R.layout.custom_spinner_list);
                  adapter.setDropDownViewResource(R.layout.customer_spinner);
                  spin.setAdapter(adapter);
      

      custom_spinner_list.xml

      <TextView xmlns:android="http://schemas.android.com/apk/res/android"
          android:text="@+id/TextView01"
          android:id="@+id/TextView01"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:textSize="12dp"
          android:textColor="#000000">
      </TextView>
      

      customer_spinner.xml

      <TextView xmlns:android="http://schemas.android.com/apk/res/android"
          android:text="@+id/TextView01"
          android:id="@+id/TextView01"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:textSize="12dp"
          android:textColor="#000000"
          android:height="42dp"
          android:gravity="center_vertical"
          android:padding = "2dp">
      </TextView>
      

      【讨论】:

        【解决方案3】:

        也许这已经很晚了,但我还是会分享这个,因为我遇到了类似的问题。

        您需要为 Spinner 本身使用视图“simple_spinner_item”,为下拉菜单使用“simple_spinner_dropdown_item”。

        这是一个代码sn-p:

        Spinner spnCat = (Spinner) findViewById(R.id.idea_category);
        
        Cursor c = myDBHelper.getCategories();
        startManagingCursor(c);
        
        if (c != null) {
            // use the simple_spinner_item view and text1
            SimpleCursorAdapter adapterCat = new SimpleCursorAdapter(this,
                android.R.layout.simple_spinner_item,
                c, 
                new String[] {c.getColumnName(1)}, 
                new int[] {android.R.id.text1});
        
            spnCat.setAdapter(adapterCat);
        
            // use the simple_spinner_dropdown_item
            adapterCat.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);
        }
        

        希望对您有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-05-13
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多