我正在学习如何使用Fragments,并尝试在运行时将片段tag_button.xml
添加到另一个布局tagFragmentContainer
深处的FrameLayout deepLayout.xml
。应用程序现在崩溃并出现错误:"在onSaveInstanceState"之后无法执行此操作在deepLayoutActivity
中提到的最后一行,在案例陈述中。
tagFragmentContainer
,位于大型布局文件中间的LinearLayout中,deepLayout.xml
:
<LinearLayout
android:id="@+id/tagButtonsLayout"
android:layout_marginBottom="10dip"
android:visibility="gone"
style="@style/Form">
<FrameLayout
android:id="@+id/tagFragmentContainer"
android:layout_marginBottom="10dip"
style="@style/Form">
</FrameLayout>
</LinearLayout>
在deepLayoutActivity
中,当选择布局中的某个选项时,我会显示tagButtonsLayout
并添加TagButtonFragment
。我需要能够多次这样做:
final LinearLayout lm = (LinearLayout) findViewById(R.id.tagButtonsLayout);
lm.setVisibility(View.VISIBLE);
// Create a new Fragment to be placed in the activity layout
TagButtonFragment firstFragment = new TagButtonFragment();
// Add the fragment to the 'tagFragmentContainer' FrameLayout
getSupportFragmentManager().beginTransaction().add(R.id.tagFragmentContainer, firstFragment, "tagOne").commit();
TagButtonActivity
已不再使用:
public class TagButtonActivity extends FragmentActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.deepLayout);
if (savedInstanceState != null)
return;
TagButtonFragment firstFragment = new TagButtonFragment();
getSupportFragmentManager().beginTransaction().add(R.id.tagFragmentContainer,
firstFragment, "tagOne").commit();
}
}
TagButtonFragment
:
public class TagButtonFragment extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState)
{
// Inflate the layout for this fragment
return inflater.inflate(R.layout.tag_button, container, false);
}
}
tag_button.xml
:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/Form">
<TextView
android:id="@+id/tag_button_header"
style="@style/FieldHeader"
android:text="example text"/>
<RelativeLayout
android:id="@+id/tag_button_block"
android:layout_width="match_parent"
android:layout_marginLeft="2dip"
android:layout_marginTop="3dip"
android:layout_marginBottom="3dip"
android:layout_marginRight="2dip"
android:layout_height="43dip"
android:clickable="true"
android:background="@drawable/row_spinner_selector">
<ImageView
android:id="@+id/question_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/arrow_right"/>
<TextView
android:id="@+id/tag_question_text"
android:layout_toLeftOf="@id/question_arrow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_centerVertical="true"
android:textColor="@android:color/black"
android:singleLine="true"
android:ellipsize="end"
android:textSize="15sp"
android:text="@string/not_selected"/>
</RelativeLayout>
</LinearLayout>
我当然感谢任何帮助从知道比我更好的碎片的人那里得到这个帮助!
谢谢!
答案 0 :(得分:0)
首先,当您想要通过XML创建Fragment时,您可以将其放在View容器中,或者通过代码执行它(就像您使用getSupportFragmentManager().beginTransaction().add(R.id.tagFragmentContainer, firstFragment, "tagOne").commit();
所做的那样)
接下来,你是用一个片段给布局充气,用另一个片段给布局充气,用另一个片段给布局充气,等等,每个布局都是空的(你看问题了吗?)。请注意,xmlns:tools="http://schemas.android.com/tools"
命名空间仅用于图形布局查看器,它不用于对片段中的布局进行膨胀,因此tools:layout="@layout/tag_button"
除了在图形布局查看器中显示布局之外什么都不做,而不是真正膨胀任何内容
因此,对布局进行充气的正确方法是:
public class TagButtonFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.tag_button, container, false);
}
}