【问题标题】:Bug with Android spinner in 2.2 related to layout arrangement2.2 中与布局排列相关的 Android 微调器的错误
【发布时间】:2011-05-31 17:31:44
【问题描述】:

以下是我在使用 Android 应用程序时遇到的错误的链接。与其试图通过巨大的文字墙来解释它,我认为一个简单的视频会更直接、更容易理解。

http://www.youtube.com/watch?v=9V3v854894g

我已经在这个问题上苦苦挣扎了一天半了。我只是发现它可以通过最近更改 XML 布局来解决,这对我来说完全没有意义。我不知道如何正确修复它,或者解决问题的方法,因为我需要在我的应用程序中使用嵌套布局。

感谢大家的帮助!

代码如下:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class Builder extends Activity {
    private Spinner mCompSelect;
    private Spinner mNameSelect;
    private int[] mCompColorAsBuilt;
    private int mComponent;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.builder);

        mCompColorAsBuilt = new int[3];

        //Attach our objects
        mCompSelect = (Spinner) findViewById(R.id.component);
        mNameSelect = (Spinner) findViewById(R.id.component_name);

        //Attach an adapter to the top spinner
        ArrayAdapter<CharSequence> a = ArrayAdapter.createFromResource(this, R.array.cc_components, android.R.layout.simple_spinner_item);
        a.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mCompSelect.setAdapter(a);
        //Create a listener when the top spinner is clicked
        mCompSelect.setOnItemSelectedListener(new OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //Save the position
                mComponent = position;
                //Create a new adapter to attach to the bottom spinner based on the position of the top spinner
                int resourceId = Builder.this.getResources().getIdentifier("component"+Integer.toString(mComponent)+"_color", "array", Builder.this.getPackageName());      
                ArrayAdapter<CharSequence> a = ArrayAdapter.createFromResource(Builder.this, resourceId, android.R.layout.simple_spinner_item);
                a.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                mNameSelect.setAdapter(a);
                //Set the position of the bottom spinner to the saved position
                mNameSelect.setSelection(mCompColorAsBuilt[mComponent]);
            }
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        //Attach an adapter to the bottom spinner
        int resourceId = this.getResources().getIdentifier("component"+Integer.toString(mComponent)+"_color", "array", this.getPackageName());      
        ArrayAdapter<CharSequence> b = ArrayAdapter.createFromResource(this, resourceId, android.R.layout.simple_spinner_item);
        b.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mNameSelect.setAdapter(b);
        mNameSelect.setOnItemSelectedListener(new OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {       
                //Save the position of the bottom spinner
                mCompColorAsBuilt[mComponent] = position;
            }
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }
}

XML

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

    <Spinner
        android:id="@+id/component"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/finish"
        android:drawSelectorOnTop="true"
        android:prompt="@string/component_spinner" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <Spinner
            android:id="@+id/component_name"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawSelectorOnTop="true"
            android:prompt="@string/component_name_spinner" />
    </LinearLayout>
</RelativeLayout>

【问题讨论】:

    标签: java android layout


    【解决方案1】:

    作为一个黑客,尝试在受影响的Spinner 上调用invalidate()。首先,请在致电setSelection() 后尝试。如果失败,请尝试在 Spinner 上使用 postDelayed() 稍后(例如 50 毫秒)调用 invalidate()

    此外,我鼓励您创建一个演示项目,其中包含两个活动(或者可能只是一个具有两个布局的活动)来说明此行为,并将其和解释发布到http://b.android.com

    【讨论】:

    • 我都尝试了,但不幸的是以相同的结果结束。你确实给了我一个解决问题的想法。我可以将底部微调器设置为错误的值并使用 postDelayed() 将其设置回正确的值。它并不漂亮,但它现在似乎已经成功了。这个周末我会整理一份错误报告。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-25
    • 1970-01-01
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多