列表查看项目不显示在android选项卡式片段活动
问题描述:
我无法弄清楚代码中有什么问题,我的列表中的项目查看从选项卡式活动调用的片段上的项目不显示任何项目从我的字符串数组中。列表查看项目不显示在android选项卡式片段活动
我已经设置了文本视图和列表视图的背景,以查看它是否实际上已经显示,并且它确实如此。我可以在下方列表视图的顶部和黄色背景中看到textview。
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>
这里是在的onCreate方法的Java代码DisplayQuestionsFragment.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;
}
没有错误返回它只是该项目不在列表视图中。请帮助并提供一些意见。
非常感谢!
答
试试这个,
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
注:有一点需要注意的是,而不是使用字符串数组使用字符串数组列表可能会解决这个问题一劳永逸。
调用另一个重写方法ÓnViewCreated并将其放入其中并尝试 –