【问题标题】:Android spinner is not left aligned when set to wrap_content设置为 wrap_content 时,Android 微调器未左对齐
【发布时间】:2012-05-06 23:33:14
【问题描述】:

我在 2.3 上的微调器:

在 4.0 上:

这是我的代码:

Spinner s = (Spinner) findViewById(R.id.spinner);
    ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(
            this, R.array.planets, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    s.setAdapter(adapter);

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="10dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="Please select a planet:"
    />

    <Spinner 
        android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="true"
    />
</LinearLayout>

还有数组资源,

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="planets">
        <item>a</item>
        <item>aa</item>
        <item>aaa</item>
        <item>aaaa</item>
        <item>aaaaa</item>
        <item>aaaaaa</item>
        <item>aaaaaaa</item>
        <item>aaaaaaaa</item>
    </string-array>
</resources>

我意识到,在 2.3 上,微调器布局宽度将基于微调器项目内容,而因为它修复了最宽的内容,并且在 4.0 上不会更改其他内容。如果spinner布局设置为fill_parent是没有问题的,但是我需要一个wrap_content spinner...

您知道如何使其在两个系统上保持一致吗?因为当内容长度非常不同时它看起来很丑。

谢谢。

【问题讨论】:

  • @Agarwal 布局文件已添加 :)

标签: android layout operating-system spinner alignment


【解决方案1】:
  1. 您可以尝试使用设置为微调器的布局权重。

  2. 另一种可能性是微调器的 setwidth 以编程方式表示设备宽度的一半或设备宽度的三分之一。

【讨论】:

  • 您的意思是在选择项目后尝试设置布局宽度,我尝试使用下面的代码 s.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView> arg0, View arg1, int arg2 , long arg3) { String item = arg0.getSelectedItem().toString(); int width = item.length() * 20 + 50; arg0.getLayoutParams().width = width; arg0.invalidate(); } }) ;但运气不好,因为微调器已经绘制,布局宽度只会影响下一个选择。
猜你喜欢
  • 2017-04-07
  • 2012-05-24
  • 2014-06-03
  • 2016-04-11
  • 1970-01-01
  • 1970-01-01
  • 2011-11-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多