2012-05-02 15 views
7

私はAndroidにはかなり新しいので、明らかな答えがあるかもしれませんが、見つけられないようです。Android:アクティビティにフラグメントを追加する

私は電卓アプリのバージョンを書いています。ユーザーがボタンの1つをクリックすると、フラグメントが開始され(より多くのボタンを使用してより多くの入力を行うことができるようにしたい)次のように

フラグメントのコードは次のとおりです。

import android.app.Fragment; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class VariableMenu extends Fragment { 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState);} 

    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

    } 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     return inflater.inflate(R.layout.fragment, container, false); 
} 

} 

XMLレイアウトで、次のように:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

<Button 
     android:id="@+id/Bx" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="X" 
     android:onClick="onButtonClicked"/> 
    <Button 
     android:id="@+id/By" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/Bx" 
     android:layout_alignTop="@id/Bx" 
     android:text="Y" 
     android:onClick="onButtonClicked"/> 
    <Button 
     android:id="@+id/Bz" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/By" 
     android:layout_alignTop="@id/By" 
     android:text="Z" 
     android:onClick="onButtonClicked"/> 

</RelativeLayout> 

(私はハードコーディングされた文字列を使用すべきではないけど、私'LL

活性化ボタンのonTouchメソッドを使用して断片化トランザクションを使用して追加してみました:

public void onFragmentActivated(View v) { 
     FragmentManager fragmentManager = getFragmentManager(); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
     VariableMenu fragment = new VariableMenu(); 
     fragmentTransaction.add(R.layout.main, fragment); 
     fragmentTransaction.commit(); 
    } 

しかし、「VariableMenuフラグメントのIDが見つかりません:...(メイン)」というエラーが表示されました。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

(Irrelevant things) 

    <fragment 
     android:id="@+id/Fragment" 
     android:name="calculator.app.VariableMenu" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/B1"/> 

(Irrelevant things) 

</RelativeLayout> 

しかし、これはすぐに活動を作成したとして、フラグメントがそこにいるの結果(とsetContentView(R.layout.main):

だから、私はメインのXMLファイルでのビューを作っ;走った)。

UIを持つフラグメントをプログラムで追加してアクティビティに追加する簡単な方法はありますか?

答えて

11

ここで問題となるのは、フラグメントには、ビューIDを持つコンテナが必要であることです。レイアウトIDを指定すると、エラーが発生します。

フラグメントを相対レイアウトに追加できますが、適切に配置するには適切なレイアウトパラメータを割り当てる必要があります。そのコンテンツをラップする相対レイアウト内にFrameLayoutを作成してそこにフラグメントを追加する方が簡単です。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <FrameLayout 
     android:id="@+id/FragmentContainer" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/B1"/> 

</RelativeLayout> 

その後

fragmentTransaction.add(R.id.FragmentContainer, fragment); 
+1

+1。 @Spencerまた、あなたのフラグメントが別個のアクティビティとして動作できる場合、それを変更してFragmentActivityを拡張し、それを通常のアクティビティのように扱うことができます – Joe

+0

ありがとう、それは完全に機能しました。この場合、FragmentActivityは機能しないとは思いませんが、将来のためにそのオプションを念頭に置いていきます。 – Spencer

関連する問題