今日は、フラグメント アクティビティを使用してプロジェクトを作成してみます。MainActivity エクステント FragmentActivity があります。MainActivity にはレイアウトがあります。
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dip"
android:background="#000" />
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
レイアウトには 2 つのボタンがあり、ここに Fragment を置き換えるための FrameLayout があります。MainActivity onCreate にフラグメントを挿入します。
MainActivity.java
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((Button) findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.e("Button", "click--------------");
}
);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, fragment).addToBackStack(null).commit();
}
}
R.id.frame_layout にフラグメントを挿入すると、button1 をタッチしようとしますが、応答しません。logcat に表示されない
私を助けてください!ありがとう