我試圖通過從onCreateView(LayoutInflater, ViewGroup, Bundle)
返回我自己的視圖層次結構來自定義片段佈局。這膨脹了我的自定義視圖,但似乎堆疊的意見,而不是內部膨脹,我看到一切。任何幫助表示讚賞。Android +具有自定義視圖層次結構的ListFragment
MyActivity.java:
public class MyActivity extends FragmentActivity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
ArrayListFragment list = new ArrayListFragment();
getSupportFragmentManager().beginTransaction().add(android.R.id.content, list).commit();
}
}
public static class ArrayListFragment extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
inflater.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.main, container);
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String List[] = {"Larry", "Moe", "Curly"};
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, List));
}
}
}
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="THIS IS A BUTTON" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false" />
<TextView
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data" />
</LinearLayout>
另外要注意,我使用的是Android的支持-V4。 從Android文檔,它明確規定: 「ListFragment具有由單個列表 視圖的默認佈局。然而,如果你願意,你可以通過 自定義片段佈局從onCreateView返回自己的視圖層次結構(。 LayoutInflater, ViewGroup,Bundle)爲此,您的視圖層次結構必須包含一個ID爲「@android:id/list」的 ListView對象(或者列表如果它位於 代碼中)。 – worked