2017-11-23 7 views
0

私は文字通り私のツールバーで私の背中の矢印を取得しようとするすべてを試したが、私は失敗しましたオーバーライドしようとしましたonOptionsItemSelected私はまたスレッドUp Navigation (Action Bar's back arrow) is not working for fragmentsと成功しません。私はまた断片で働いています。Androidツールバーback arrow not clickable

RegisterActivity

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener { 

private Toolbar toolbar; 

@Override 
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    //setContentView(R.layout.activity_register); 
    ButterKnife.bind(this); 

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    getSupportActionBar().setDisplayShowHomeEnabled(true); 
    getSupportActionBar().setHomeButtonEnabled(true); 
} 

@Override 
public void onClick(View view) { 
    switch (view.getId()) { 
     case (R.id.button_accept_tos) : 
      /* 
      Fragment inputUserFragment = new AcceptAgreementFragment(); 
      FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
      transaction.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out); 
      transaction.replace(R.id.main_activity, inputUserFragment); 
      transaction.addToBackStack(null); 
      transaction.commit(); 
      */ 
      break; 
    } 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case android.R.id.home: 
      onBackPressed(); 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
    } 
} 

マニフェスト

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:roundIcon="@mipmap/ic_launcher_round" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".activity.MainActivity" 
     android:screenOrientation="portrait"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity android:name=".activity.RegisterActivity" 
     android:windowSoftInputMode="adjustPan|adjustResize" 
     android:screenOrientation="portrait"> 
    </activity> 

    <meta-data 
     android:name="android.support.PARENT_ACTIVITY" 
     android:value=".activity.MainActivity" 
     /> 

</application> 

と私のactivity_register XML

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 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" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:background="@color/ghostWhiteColor" 
    android:fitsSystemWindows="true" 
    tools:context="com.dae.market.dav.lux.activity.RegisterActivity"> 


    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:minHeight="?attr/actionBarSize" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:titleTextColor="@android:color/white" 
     app:navigationIcon="@drawable/ic_arrow_back_white_24dp" 
     android:background="?attr/colorPrimary"> 

     <TextView 
      android:id="@+id/toolbar_title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="test" 
      android:textColor="@android:color/white" 
      style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title" 
      android:layout_gravity="center" 
      /> 

    </android.support.v7.widget.Toolbar> 



    <!--wizard>--> 
    <FrameLayout 
     android:id="@+id/fragment_register" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

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

があなたの 'onClick'方法がこれまでに呼び出されますでしょうか?ツールバー

toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onBackPressed(); } }); 

ためsetNavigationOnClickListenerを呼び出しますか(コメントアウトされたものはデバッグからのものです) – Phix

+0

@Phixはいそれは – user8924538

+0

@ user8924538と呼ばれていますあなたはそれを解決しましたか? – Prem

答えて

0

追加このマニフェスト活動へ

android:theme="@style/AppTheme.NoActionBar"> 

あなたのスタイルファイルは、あなたがこのようなナビゲーション、バックボタンのイベントをクリックして取得することができ、この

<!-- Base application theme. --> 
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <!-- Customize your theme here. --> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> 
</style> 

<style name="AppTheme.NoActionBar"> 
    <item name="windowActionBar">false</item> 
    <item name="windowNoTitle">true</item> 
</style> 

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /> 

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /> 
0

のようにする必要があります。

Toolbar mToolbar = (Toolbar)findViewById(R.id.toolbar); 
setSupportActionBar(mToolbar); 
getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
mToolbar.setNavigationOnClickListener(new View.OnClickListener() { 
    @Override public void onClick(View v) { 
    // perform whatever you want on back arrow click 
    } 
}); 
0

<style name="AppTheme.NoActionBar"> 
     <item name="windowActionBar">false</item> 
     <item name="windowNoTitle">true</item> 
</style> 
あなたの活動に

Toolbar toolbar; 

//のonCreate

toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
(マニフェスト)

があなたの活動にこのテーマを使用して...これを試してみてください3210

[今すぐ戻るボタンをonClickのを確認...