微调ArrayAdapter与自定义布局崩溃 [英] Spinner ArrayAdapter crashing with custom layout
问题描述
我想用一个自定义的微调/ ArrayAdapter实验。以下是我的code:
I am trying to experiment with a custom Spinner / ArrayAdapter. Following is my code:
myspinner=(Spinner)findViewById(R.id.myspinner);
myadapter=new ArrayAdapter<String>(this,R.layout.mytextview,R.id.mytv,sample_data);
myadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
myspinner.setOnItemSelectedListener(this);
myspinner.setAdapter(myadapter);
下面是一个包含MYTV自定义布局mytextview:
Following is the custom layout mytextview that contains mytv:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:id="@+id/mylayout" android:layout_width="match_parent" android:layout_height="wrap_content">
<View android:layout_width="match_parent" android:layout_height="15dip" android:id="@+id/myview1" android:background="@color/bordertop"></View>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="30dip" android:id="@+id/mytv"></TextView>
<View android:layout_width="match_parent" android:layout_height="15dip" android:id="@+id/myview2" android:background="@color/borderbottom"></View>
</LinearLayout>
当我是preSS微调带来的弹出,Android的崩溃。该逻辑来工作完全正常,当我注释掉setDropDownViewResource方法。我还是不明白什么看法setDropDownViewResource实际上是设置。谁能告诉我怎么看起来像在默认情况下。而且,如果一个人想用自定义的布局覆盖吗? - 不布局需要有一个特定的结构
When i'm press the spinner to bring the popup, Android crashes. The came logic works perfectly fine, when I comment out the setDropDownViewResource method. I still don't understand what view setDropDownViewResource actually sets. Can anyone show me how it looks like by default. And, if one wants to override it with a custom layout - does the layout need to have a specific structure?
推荐答案
下面是我通常用于纺纱:
here is what i have typically used for spinners:
Spinner distancespinner = (Spinner) findViewById(R.id.distance);
ArrayAdapter<CharSequence> distanceadapter = ArrayAdapter.createFromResource(
this, R.array.distance, android.R.layout.simple_spinner_item);
distanceadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
distancespinner.setAdapter(distanceadapter);
distancespinner.setOnItemSelectedListener(new distanceSelector());
(显然替换任何你想要的距离)
(obviously replace distance with whatever you want)
<Spinner
android:id="@+id/distance"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:prompt="@string/app_name"
/>
我,它看起来像你的问题是在这一行:
to me it looks like your problem is in this line :
myadapter=new ArrayAdapter<String>(this,R.layout.mytextview,R.id.mytv,sample_data);
但后来我不知道,你的 ArrayAdapter
基于而是因为我有和按的微调的android.R.layout.simple_spinner_item ID为参考标准微调外观布局,由平台定义,然后setDropDownViewResource( INT)被调用来定义每个项目的外观小部件打开时(simple_spinner_dropdown_item是由平台定义的另一个标准布局)。
but then i dont know, your ArrayAdapter
is based on instead of as i have and as per Spinner "the android.R.layout.simple_spinner_item ID references a layout for the standard spinner appearance, defined by the platform. Then setDropDownViewResource(int) is called to define the appearance for each item when the widget is opened (simple_spinner_dropdown_item is another standard layout defined by the platform)."
我觉得如果你想自定义您的微调, .setDropDownViewResource();
是在哪里做的。
i think if you want to customize your spinner, .setDropDownViewResource();
is where to do it.
这篇关于微调ArrayAdapter与自定义布局崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!