私は基本的な Fragments のものをテストしているので、4 つのクラス、1 つの MainActivity、1 つの FragmentActivity、2 つの Fragments を作成しました。アイデアは、MainActivity からボタンを介して FragmentActivity を呼び出すことであり、Fragment1 と Fragment2 が 30% の Fragment1 として表示されます。 orrange の背景と Fragment 2 の画面の背景が青の 70% です。エラーはありません。MainActivity のボタンをクリックすると FragmentActivity に移動しますが、テキストビューのみの白い画面が表示されますが、 FragmentActivity の .xml ファイルに、Orange-Blue 画面が表示されますか?
ここに MainActivity 部分があります
buttonFrag2.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,StaticFragment.class);
startActivity(intent);
}
});
次に、StaticFragment クラス
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
public class StaticFragment extends FragmentActivity {
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
}
}
および static_fragment.xml 、前述のようにグラフィカルレイアウトに正しく表示されます
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false"
>
<fragment
android:name="com.example.fragment.ListFrag"
android:id="@+id/listFrag"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight=".3"
tools:layout="@layout/list_frag" />
<fragment
android:name="com.example.fragment.DetailFrag"
android:id="@+id/detailFrag"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight=".7"
tools:layout="@layout/detail_frag" />
</LinearLayout>
Fragmentクラスの例としても(それらは同一です)
package com.example.fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ListFrag extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.list_frag, container, false);
}
}