【问题标题】:ListView items not displaying in android tabbed fragment activityListView 项目未显示在 android 选项卡式片段活动中
【发布时间】:2016-11-05 05:45:10
【问题描述】:

我无法弄清楚从选项卡式活动调用的片段上的列表视图中的项目没有显示我的字符串数组中的任何项目的代码有什么问题。

我已经设置了文本视图和列表视图的背景,以查看它是否已实际显示,并且确实显示了。我可以看到顶部的文本视图和下面列表视图的黄色背景。

fragment_display_questions.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="layout.DisplayQuestionsFragment">

<!-- TODO: Update blank fragment layout -->

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView2"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="18dp"
        android:background="#ffff00"/>

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="333dp"
        android:id="@+id/listview"
        android:layout_below="@+id/textView2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="#ffff00"
        android:layout_marginTop="156dp" />

</RelativeLayout>

这里是DisplayQuestionsFragment.java的onCreate方法中的java代码

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view =  inflater.inflate(R.layout.fragment_display_questions, container, false);

    String[] questions={"Question 1","Question 2"};


    ListView lv = (ListView) view.findViewById(R.id.listview);

    ArrayAdapter<String> listviewadapter = new ArrayAdapter<String>(
            getActivity(),
            android.R.layout.simple_list_item_1,
            questions
    );

    lv.setAdapter(listviewadapter);

    return view;
}

没有错误返回,只是项目不在列表视图中。请帮助并提供一些意见。

非常感谢!

【问题讨论】:

  • 调用另一个覆盖方法ÓnViewCreated并将您的代码放入其中并尝试

标签: listview android-studio android-fragments android-fragmentactivity


【解决方案1】:

试试这个,

android:layout_height="wrap_content"
android:layout_marginTop="10dp"

注意:需要注意的一点是,不要使用字符串数组,而是使用字符串数组列表,它可以一劳永逸地解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多