【问题标题】:Spinner Showing in XML but Not When Loading App微调器以 XML 显示,但在加载应用程序时不显示
【发布时间】:2016-11-06 16:46:08
【问题描述】:

我的片段中有以下内容,我正在尝试创建一个将数字 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()返回rootview。正如你现在所拥有的,你正在返回一个不同的、新膨胀的、未初始化的View
  • 谢谢!想要作为答案提交并且我可以将其标记为已接受?

标签: java android xml android-layout


【解决方案1】:

你必须像这样返回rootView

@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 rootview;
}

【讨论】:

    【解决方案2】:

    试试:

    return rootview;
    

    代替

    return inflater.inflate(R.layout.fragment_create, container, false);
    

    【讨论】:

      【解决方案3】:

      在您的onCreateView() 方法中,您将在return 语句中返回一个新的、未初始化的View。相反,您想返回在此之前膨胀和初始化的View。即把return语句改成:

      return rootview;
      

      【讨论】:

        猜你喜欢
        • 2019-11-16
        • 2017-07-29
        • 2013-09-24
        • 1970-01-01
        • 1970-01-01
        • 2013-07-21
        • 2014-03-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多