2017-05-30 5 views
0

私はfloatActionButtonを持っており、活動全体に再利用したいので、私はこのような何かを持っている現時点ではonclickの方法、リユースfloatActionButtonアンドロイド

を呼び出すために、各活動に必要はありません。

ヘルパー

public class FloatButtonToolbar 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) { 
       Log.d("ENTERED","GOTIT"); 
      Intent i = new Intent(FloatButtonToolbar.this,CameraCapture.class); 
       startActivity(i); 
      } 
     }); 
    } 

} 

メインXML(私は再利用したい1)

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.afcosta.inesctec.pt.android.Helpers.FloatButtonToolbar"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

    </android.support.design.widget.AppBarLayout> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:layout_margin="@dimen/fab_margin" 
     app:srcCompat="@android:drawable/ic_dialog_email" /> 

</android.support.design.widget.CoordinatorLayout> 

今、私の活動のxmlに私はこれを持っている:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fitsSystemWindows="true" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.afcosta.inesctec.pt.android.FamilyLibrary" 
    android:paddingTop="6dp"> 


    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:layout_margin="@dimen/fab_margin" 
     app:srcCompat="@android:drawable/ic_menu_camera" 
     app:backgroundTint="#f1c40f" 
     tools:layout_constraintRight_creator="1" 
     tools:layout_constraintBottom_creator="1" 
     app:layout_constraintBottom_toBottomOf="parent" 
     android:layout_marginEnd="37dp" 
     app:layout_constraintRight_toRightOf="parent" 
     android:layout_marginBottom="33dp" /> 

事は上記の最初の例である、私はonclickのリスナーを持っているが、それは動作していない、それがonclickの中に入りませんログは表示されません。S

任意の摂取ですか?

+0

- :
次の参照を使用することができますフラグメントの詳細については。使用しているIDEは**あなたのアプリの問題とは関係ありません**。 –

答えて

1

複数のアクティビティで同じビューを再利用することはできません。ビューはアクティビティに属します。各アクティビティにonClickを設定する必要があります。あなたができることは、コンストラクタでonClickの動作をハードコードするFloatingActionButtonのカスタム子を作ることです。

3

ビューは、アクティビティ全体で使用することはできませんが、断片化することができます。したがって、アンドロイドで特定のビューやビューグループに複数のアクティビティからアクセスできるようにする必要がある場合は、それらのビューグループの断片を作成し、それらのビューグループをアクティビティ間で使用できます。したがって、この場合は、ファブを含むフラグメントを作成し、そのフラグメントの各アクティビティレイアウトで右下隅を予約し、アクティビティが作成されたときにそこにフラグメントを配置することができます。すべての投稿には無関係 `アンドロイド-studio`タグを追加を停止してください

https://developer.android.com/guide/components/fragments.html

https://developer.android.com/training/basics/fragments/index.html

関連する問題