這可能是twick我失蹤,請協助Android的ListView控件:不能看到一個簡單的列表
我已經創建一個簡單的列表,並將其綁定到一個適配器
,但由於某種原因,在設備上運行時,不顯示
這是我遇到的所有教程對準
fragment_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:paddingTop="4dip"
android:paddingBottom="6dip"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="13sp"
android:weightSum="1.0"
android:orientation="horizontal"
tools:showIn="@layout/activity_main">
<TextView android:id="@+id/singName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="LOGO"
android:layout_gravity="top"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="40dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView
android:layout_width="280dp"
android:layout_height="match_parent"
android:id="@+id/messagesList"
android:layout_gravity="center_vertical|left"
android:visibility="visible"
android:layout_above="@+id/bottomTxt"
android:layout_alignParentStart="true"
android:layout_below="@+id/singName" />
的Java:
public class MainActivityFragment extends Fragment {
public MainActivityFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
ListView lv = (ListView) rootView.findViewById(R.id.messagesList);
String [] messages = new String[]{
"Item 1",
"Item 2",
"Item 3",
"Item 4"
};
ArrayAdapter<String> adapter;
adapter = new ArrayAdapter<>(
getActivity(),
R.layout.message_item,
messages);
lv.setAdapter(adapter);
return rootView;
}
message_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="50dp"
android:layout_height="50dp"
android:minHeight="?android:listPreferredItemHeightLarge"
android:gravity="center_vertical"
android:textSize="20dp"
android:id="@+id/message_item_text">
</TextView>
「fragment.xml」真的名爲'fragment_main.xml',就像你正在膨脹的那個? – mikeorr85
是的,它是fragment_main.xml – HaimS8