是否可以在佈局xml中填充微調器的選項? This page建議我應該使用ArrayAdapter?它似乎尷尬不能做到這一點..直接在佈局中填充微調器xml
回答
我不知道這件事,但給它一個鏡頭。
在strings.xml定義:
<string-array name="array_name">
<item>Array Item One</item>
<item>Array Item Two</item>
<item>Array Item Three</item>
</string-array>
在您的佈局:
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:entries="@array/array_name"
/>
我聽說這並不總是對設計師的工作,但它編譯罰款。
定義這個在你String.xml文件 和數組的名字是你想要的,如重量
<string-array name="Weight">
<item>Kg</item>
<item>Gram</item>
<item>Tons</item>
</string-array>
,這代碼你layout.xml
<Spinner
android:id="@+id/fromspin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="@array/Weight"
/>
在你的Java文件如果你在活動中寫入該代碼,然後刪除getactivity,則在片段中使用交疊性
a = (Spinner) findViewById(R.id.fromspin);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this.getActivity(),
R.array.weight, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
a.setAdapter(adapter);
a.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (a.getSelectedItem().toString().trim().equals("Kilogram")) {
if (!b.getText().toString().isEmpty()) {
float value1 = Float.parseFloat(b.getText().toString());
float kg = value1;
c.setText(Float.toString(kg));
float gram = value1 * 1000;
d.setText(Float.toString(gram));
float carat = value1 * 5000;
e.setText(Float.toString(carat));
float ton = value1/908;
f.setText(Float.toString(ton));
}
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
});
// Inflate the layout for this fragment
return v;
}
這對我更好。謝謝! – 2016-09-21 20:40:42
關於第一條評論:如果你這樣做,你會得到一個錯誤(在Android Studio中)。這是關於它脫離Android命名空間。如果您不知道如何解決此錯誤,請查看下面的示例。希望這可以幫助!
例-Before:
<string-array name="roomSize">
<item>Small(0-4)</item>
<item>Medium(4-8)</item>
<item>Large(9+)</item>
</string-array>
示例 - 後:
<string-array android:name="roomSize">
<item>Small(0-4)</item>
<item>Medium(4-8)</item>
<item>Large(9+)</item>
</string-array>
不起作用。 XML文件希望他的'string-array'具有'name'屬性,並且不能識別'android:name'。 – 2017-08-29 15:24:54
請在strings.xml中的數組,必須包含您的數據,並指出其採用了android來微調器:項XML屬性。
只是微調此圖像中替換listview
:
[http://androidride.com/wp-content/uploads/2018/01/how-android-entries-works-easiest-way-to-make-listview-in-android-androidride.com_-1024x863.png][1] 您可以找到如何安卓項工作過。
- 1. 用XML文件佈局填充佈局
- 2. 在android中填充微調器
- 3. 在Android中動態填充微調器?
- 4. Android佈局:按鈕的垂直填充
- 5. 微調器,Onitemselected,佈局
- 6. Android微調器佈局
- 7. 填充微調框
- 8. 從列表中填充微調器
- 9. 根據另一個微調器的選擇填充微調器
- 10. 基於另一個微調器填充微調器
- 11. 麻煩從ArrayList填充微調器
- 12. 用sqlite數據填充微調器
- 13. 使用ArrayAdapter填充微調器
- 14. 如何在協調器中垂直放置佈局佈局
- 15. 是否可以直接在Xamarin網格佈局中應用填充?
- 16. xml的協調器佈局
- 17. Android:容器XML佈局包裝重量和填充高度
- 18. 使用xml佈局的Android微調器數據綁定
- 19. Android - 按鈕填充佈局
- 20. Android佈局 - 填充除
- 21. 填充時刷新佈局
- 22. 在佈局中添加填充的動態佈局問題
- 23. 包含佈局在另一個佈局中填充視圖
- 24. 更改Android微調器佈局/設計
- 25. Android微調器自定義佈局
- 26. 使用微調器更改佈局
- 27. Java中的微調零填充
- 28. 在不同的線程中填充微調器
- 29. 使用AsyncTask在Android 4.2中使用JSON填充微調器
- 30. 在Android中使用SimpleAdapter填充微調器
非常好,非常感謝! – cambraca 2010-10-27 03:00:14
只是想補充一點,如果你想爲每個選擇的項目分開值,你可以指定'android:entryValues =「@ array/array_name_values」'。 – 2013-06-04 16:50:40
一個簡單的筆記......從我的理解中,Spinners不支持'drawSelectorOnTop',因爲除了Spinner本身沒有獨立的選擇器。解決方案絕對有效,謝謝! – Jabari 2013-06-11 22:25:22