我的 fragment 中有以下内容,我正在尝试创建一个显示数字 1-5 作为选择选项的微调器:
@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);
}
字符串.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()
方法中,您将在 return
语句中返回一个新的、未初始化的 View
。相反,您想要返回之前已膨胀和初始化的 View
。即,将 return
语句更改为:
return rootview;
关于java - 微调器以 XML 格式显示但在加载应用程序时不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38193404/