2016-03-30 10 views
0

最初の画面に異なる画像を背景として4つの異なるレイアウトが含まれているアプリを開発しています。私がしたいのは、あるレイアウトをクリックすると、新しいフラグメントがその背景イメージで上に表示されます。別のイメージやテキストビューを表示するために同じフラグメントを使用できますか?もしそうなら、どのように?レイアウトをクリックすると、同じ画像の新しいフラグメントが開きます

[により以下の点に画像をアップロードすることができない]

MainActivity.java

public class MainActivity extends AppCompatActivity { 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 



    } 

    // Click methods goes here 

    public void clickAboutUs(View v) 
    { 
     /*Intent aboutUs = new Intent(MainActivity.this, FragmentAboutUs.class); 
     startActivity(aboutUs);*/ 

     FragmentManager fragmentManager = getFragmentManager(); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
     Fragment fragmentAboutUs = new FragmentAboutUs(); 
     fragmentTransaction.add(R.id.container,fragmentAboutUs); 
     fragmentTransaction.commit(); 


    } 







    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 


FragmentAboutUs.java 


public class FragmentAboutUs extends Fragment 
{ 

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

     View view = inflater.inflate(R.layout.about_us, container,false); 
     return view; 
    } 
} 

もの特定の背景画像の背景画像と同じ画像で、私は新鮮な断片画面をするレイアウトをクリックオン私のdrawablesフォルダに格納されています。

[1]: http://i.stack.imgur.com/PKX4L.png 
+0

私はあなたの 'activity_main.xml'をコード' - -'で編集して欲しいと思います。 –

答えて

0

はい、同じ画像を別の画像やテキストで表示することができます。イメージリソースのIDまたはパス、および引数に表示するテキストを追加できます。以下のようにメインアクティビティからフラグメントを初期化してください。

MyFragment myFragment = new myFragment(); 
    Bundle bundle = new Bundle(); 
    bundle.putInt("image",<image resourse id>); 
    bundle.putString("text1",<your string1>); 
    bundle.putString("text2",<your string2>); 
    myFragment.putArgs(bundle); 

そしてOnCreateイベントにご自分のフラグメント内部またはONcreateView バンドルバンドル= getArguments(以下のようにデータを取得します)。 imageId = bundle.getInt( "image"); text1 = bundle.getString( "text1"); text2 = bundle.getString( "text2");

これが役に立ちます。疑いのあることは私に知らせてください。

+0

私のコードを見てください。 –

+0

私の答えを使ってあなたの断片に合わせてカスタマイズしてください。それは動作する必要があります –

+0

大丈夫感謝私はそれを試してみます –

関連する問題