【问题标题】:Text in a spinner changes height and width selecting another item of the spinner微调器中的文本更改高度和宽度,选择微调器的另一个项目
【发布时间】:2015-06-27 12:38:42
【问题描述】:

我为 Android 微调器编写了这段代码:

  <Spinner
    android:id="@+id/lstCommunity"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/community_string"
    android:layout_alignLeft="@+id/txtName"
    android:layout_below="@+id/txtName"
    android:minHeight="0dp"
    android:textSize="10sp"
    android:background="@drawable/box" />

但是在选择列表中的另一个项目后,微调器中的文本会更改高度和宽度。

所以,最初它比微调器的高度和宽度要小一些,在选择另一个项目后,它会变得比微调器的尺寸大。

我应该如何更改我的代码?

第一次编辑: 我正在尝试使用微调器从列表中选择一些文本。

(我无法发布图片)

文本最初适合微调器,但在下拉菜单中选择另一个选项后,文本变得比微调器框大。

编辑 2: 微调器相关的Java代码:

Spinner mySpinner = (Spinner) findViewById(R.id.lstCommunity);
ArrayAdapter<CharSequence> adapter =ArrayAdapter.createFromResource(this, R.array.array1, android.R.layout.simple_spinner_item); 
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(adapter);

【问题讨论】:

  • 您能否说明您正在尝试做什么,并可能添加一些代码。
  • 看起来您的布局可能比实际需要的要复杂一些。 “对齐底部”的意思是“将我的底部与这个底部对齐”。我建议删除那个。我还建议删除 minHeight 0dp (这有什么意义?高度永远不会是负数)。如果您发布整个布局,而不仅仅是这个微调器,也会很有帮助。
  • @argablarga 最小高度是因为通常 Android 中的小部件的最小高度不适合我的布局,所以我添加了“最小高度”行
  • 请发布您与微调器相关的活动代码

标签: android eclipse text spinner


【解决方案1】:

1) 在 res/layout 中创建 spinnertext.xml

例如:

  xml version="1.0" encoding="utf-8"?>
 <TextView xmlns:android="http://schemas.android.com/.    apk/res/android"
  android:text="@+id/TextView01"
  android:id="@+id/TextView01"
  android:layout_width="wrap_content"   
  android:layout_height="wrap_content"
  android:textSize="12sp"
  android:textColor="#FFFFFFFF"
  android:gravity="center"/>

2)在文件 main.xml(在 res\layout 中)声明微调器,以管理其大小:

  <Spinner
    android:id="@+id/spinner"
    android:layout_width="fill_parent"
    android:layout_height="32dip"
    android:drawSelectorOnTop="true"
    android:prompt="@string/spinner_prompt"
     />

3) 在活动中声明微调器

 Spinner s = (Spinner) findViewById(R.id.spinner);
  ArrayAdapter adapter =   ArrayAdapter.createFromResource(this, R.array.sensors, R.layout.spinnertext);      
   adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(adapter);
 //when an item is selected call the selectSensor method
s.setOnItemSelectedListener(new OnItemSelectedListener() {
  @Override
  public void onItemSelected(AdapterView parent, View view,     int position, long id) {
        selectSensor(parent.getItemAtPosition(position));
  }
  @Override
        public void onNothingSelected(AdapterView arg0) {
        }
     });

注意 createfromresource 函数的变化。

来源:http://android2ee.blogspot.it/2011/11/spinner-managing-size-and-text.html

【讨论】:

    【解决方案2】:

    我也遇到了同样的问题。似乎有一个简单的解决方案,尝试在微调器中获得最长的选项。请注意它的大小和硬编码值,如

      <spinner
        android:layout_width="120dp"
        android:layout_height="40dp"
        android:id="@+id/spinner"/>   
    

    注意:120dp和40dp只是举例,需要根据你的例子调整

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-16
      • 2016-06-25
      • 1970-01-01
      • 2015-04-15
      • 2012-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多