创建可滚动的微调器以显示一些项目

时间:2015-07-23 21:38:47

标签: android android-spinner

我使用微调器显示项目但它只在项目少于5项时有效,当我想显示10项时发生此错误:不幸停止:(我如何使用可滚动微调器显示项目中的所有项目微调 这是: spinner.xml:和main

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

<TextView
    android:id="@+id/txt3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"

    android:text="which"
    android:textColor="#ffffff" />
 <LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<Button
    android:id="@+id/bt1help2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/Spinner01"
    android:layout_marginRight="100dp"
    android:layout_toLeftOf="@+id/txt3"
    android:text="ok"
    android:textColor="#ffffff" />

<Button
    android:id="@+id/btn2help2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="no"
    android:textColor="#ffffff" />

    </LinearLayout>

    <Spinner
    android:id="@+id/Spinner01"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:verticalScrollbarPosition="left"
    android:scrollbarThumbVertical="@drawable/scrollbar_style"
    android:layout_below="@+id/linearLayout1"
    android:layout_marginTop="22dp" />

final Dialog dl = new Dialog(Main.this);

            dl.setContentView(R.layout.spinner);

            dl.setTitle("show");
            TextView txt2 = (TextView) dl.findViewById(R.id.txt2);
            TextView txt3 = (TextView) dl.findViewById(R.id.txt3);
            ///تنظیم متن
            txt2.setText(" which one do you want to show?");

             Button btn1help2 = (Button) dl.findViewById(R.id.bt1help2);
                btn1help2.setOnClickListener(new OnClickListener() {


                    @Override
                    public void onClick(View arg0) {


            String array_spinner[];
            array_spinner=new String[5];

            array_spinner[0]="letter_one";
            array_spinner[1]="letter_2";
            array_spinner[2]="letter_3";
            array_spinner[3]="letter_4";
            array_spinner[4]="letter_5";
            array_spinner[5]="letter_6";
            array_spinner[6]="letter_7";
            array_spinner[7]="letter_8";
            array_spinner[8]="letter_9";
            array_spinner[9]="letter_10";


            Spinner s = (Spinner) dl.findViewById(R.id.Spinner01);

            ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(),android.R.layout.simple_spinner_item, array_spinner);


            s.setAdapter(adapter);

如何将其更改为可滚动的微调器?

感谢您的回答

2 个答案:

答案 0 :(得分:0)

您可以使用ScrollView。将RelativeLayout放在ScrollView内,因为它只能有一个孩子。

    <ScrollView>
      <RelativeLayout>
        .... other views
      </RelativeLayout>
    </ScrollView>

答案 1 :(得分:0)

你的问题是这个array_spinner = new String [5];你设置5个项目的大小,但你把10这样,你初始化数组这个String array_spinner []; array_spinner = new String [10];