我在片段下面,我試圖創建一個顯示數字1-5作爲選擇選項的微調:微調顯示在XML,但不是在加載應用程序
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootview = inflater.inflate(R.layout.fragment_create, container, false);
mAddImageButton = (Button) rootview.findViewById(R.id.add_image_button);
mSelectNumberofPollAnswers = (Spinner) rootview.findViewById(R.id.number_of_answers_spinner);
// Inflate the layout for this fragment
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity().getApplicationContext(),
R.array.number_of_poll_answers, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
mSelectNumberofPollAnswers.setAdapter(adapter);
return inflater.inflate(R.layout.fragment_create, container, false);
}
的strings.xml:
<string-array name="number_of_poll_answers">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
</string-array>
XML:
<Spinner
android:id="@+id/number_of_answers_spinner"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight=".3"/>
您需要返回'onCreateView()'的'rootview'。就像你現在所做的那樣,你正在返回一個不同的,新增的,未初始化的View。 –
謝謝!想要提交作爲答案,我可以將其標記爲已接受? – tccpg288