【发布时间】:2015-01-28 17:35:13
【问题描述】:
我会直接解决问题。在Android中替换片段时我做错了什么?我点击赞助商时的预期结果:
现实(我实际得到的:基本上,它只是 Sponsors 片段,它只是放在 Other 片段上):
我正在调用一个片段
fragment = new Other(this);
Sponsors.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Sponsors" />
</RelativeLayout>
Sponsors.java
...
public class Sponsors extends android.support.v4.app.Fragment {
private Context context;
public Sponsors(Context context) {
this.context = context;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View rootView = inflater.inflate(R.layout.sponsors, null);
return rootView;
}
}
其他.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/other_frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="COMMON"
android:textStyle="bold"
android:textColor="@color/header_red"
android:layout_marginTop="15dp"
android:layout_marginLeft="10dp" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="@color/header_red"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" />
<TextView
android:id="@+id/sponsors"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Sponsors"
android:textColor="@color/subtext"
android:textSize="16sp"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:paddingLeft="15dp" />
</LinearLayout>
</FrameLayout>
其他.java
public class Other extends android.support.v4.app.Fragment {
public Other(Context context) {
this.context = context;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View rootView = inflater.inflate(R.layout.other, container, false);
sponsors = (TextView) rootView.findViewById(R.id.sponsors);
sponsors.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (savedInstanceState == null) {
Sponsors duedateFrag = new Sponsors();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.other_frame_container, duedateFrag);
ft.addToBackStack(null);
ft.commit();
}
}
});
}
【问题讨论】:
-
您为什么要在其他人
Fragment的onCreateView(...)方法中添加您的赞助商Fragment?除非您使用嵌套的Fragments,否则任何个人Fragment都不应该知道或操纵任何其他Fragment。Fragments的创建、添加、替换、删除应由Activity完成。
标签: java android xml android-layout android-fragments