Android 如何添加内联ListFragment?
我有一个如下的布局。它包含一些TextView和ImageView。下面我想添加一个ListFragmentAndroid 如何添加内联ListFragment?,android,android-layout,Android,Android Layout,我有一个如下的布局。它包含一些TextView和ImageView。下面我想添加一个ListFragment <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Name: "
android:id="@+id/textView"
android:layout_gravity="left|center_vertical" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/name"
android:layout_toRightOf="@+id/textView"
android:layout_gravity="left"
android:text="Large Text" />
...
<fragment android:id="@+id/list"
android:layout_below="@id/name"
class="com.snot.bodyweightworkout.ExerciseListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
有没有关于如何在线添加的建议?查看文档
您还可以用xml中的片段替换FrameLayout
,只替换框架中的视图
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Name: "
android:id="@+id/textView"
android:layout_gravity="left|center_vertical" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/name"
android:layout_toRightOf="@+id/textView"
android:layout_gravity="left"
android:text="Large Text" />
<FrameLayout
android:id="@+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</RelativeLayout>
所以只需将listfragment放在xml中,而不是放在代码中,我不知道该怎么做。在某些情况下,我想向ProgramListFragment传递一个参数。你能给我点化一下吗?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Name: "
android:id="@+id/textView"
android:layout_gravity="left|center_vertical" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/name"
android:layout_toRightOf="@+id/textView"
android:layout_gravity="left"
android:text="Large Text" />
<fragment android:name="com.example.news.ArticleListFragment"
android:id="@+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</RelativeLayout>
FragmentManager fm = getSupportFragmentManager();
ProgramListFragment plf = (ProgramListFragment)fm.findFragmentById(r.id.myfragment);
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Name: "
android:id="@+id/textView"
android:layout_gravity="left|center_vertical" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/name"
android:layout_toRightOf="@+id/textView"
android:layout_gravity="left"
android:text="Large Text" />
<FrameLayout
android:id="@+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</RelativeLayout>
FragmentManager fm = getSupportFragmentManager();
ProgramListFragment list = new ProgramListFragment();
fm.beginTransaction().replace(R.id.list, list).commit();