답변:
나는 이것에 대해 확실하지 않지만, 그것을 쏴라.
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"
/>
나는 이것이 디자이너에서 항상 작동하지는 않지만 잘 컴파일된다고 들었습니다.
drawSelectorOnTopSpinner 자체 이외의 별도의 선택기가 없기 때문에 Spinners는 지원하지 않습니다 . 솔루션은 확실히 작동합니다. 감사합니다!
android:entryValues . ListPreference가 지원합니다.
Spinner및 의 소스 코드를보십시오 AbsSpinner. API 19와 23에서 AbsSpinner 생성자는 사용 R.styleable.AbsSpinner_entries하지만 어디에도 사용하지 않습니다 entryValues. 코드가 예상대로 작동하는 것은 우연의 일치 일뿐입니다. (또는 일부 제조업체의 ROM에는 실제로 android:entryValues?를 처리하는 사용자 정의 Spinner 구현이있을 수 있습니다 .)
이것을 String.xml 파일에 정의하고 원하는 배열의 이름을 "Weight"로 지정하십시오
<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에서 조각으로 사용됩니다. 해당 코드를 활동으로 작성하면을 제거하십시오 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;
}
첫 번째 의견과 관련하여 :이 작업을 수행하면 오류가 발생합니다 (Android Studio에서). 이것은 안드로이드 네임 스페이스를 벗어난 것과 관련이 있습니다. 이 오류를 해결하는 방법을 모르는 경우 아래 예를 확인하십시오. 도움이 되었기를 바랍니다!
예-전 :
<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>
string-array이 name속성 을 가지기를 원하고 인식하지 못합니다 android:name.
android:entryValues="@array/array_name_values".