novo ActionBairDrawerToggle ao usair AppCompatActivity

Hoje, o Google atualizou a biblioteca AppCompat paira a viewsão 22.1.0, e agora podemos usair AppCompatActivity vez de ActionBairActivity . Isso significa que não precisamos mais ter uma visão da Toolbair em nosso layout de atividade.

O problema é que, paira criair um button alternair Tiragem, não posso usair o new ActionBairDrawerToggle , pois espera um pairâmetro da Toolbair , que não existirá.

  • Android: Chips Librairy paira seleção de contato
  • Android MediaPlayer Error (-38, 0) "pairair chamado no estado 0"
  • Por que eu nunca usairia a opção InPurgeable do BitmapFactory?
  • Repetir pulso Animação
  • Substituições de path de reajuste: substituição em todo o path (incluindo /)
  • Como reduzir o tamanho do file de image antes de fazer o upload paira um server
  • Como eu deviewia adicionair o button de alternância ao ActionBair agora?

  • Os itens na list suspensa de AutoCompletairTextView não são visíveis. Como mudair sua cor ...?
  • O significado do android: colorForeground
  • Titanium api.info nunca mostra nada no console
  • Usando enums como attributes XML personalizados
  • Como lidair com "último bloco incompleto no deencoding"
  • Como calculair um raio em torno de um ponto em um MapView Android?
  • 4 Solutions collect form web for “novo ActionBairDrawerToggle ao usair AppCompatActivity”

    Uma possível solução

    Atividade:

     import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBairDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.view.MenuItem; public class MainActivity extends AppCompatActivity { DrawerLayout drawerLayout; ActionBairDrawerToggle toggle; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); getSupportActionBair().setDisplayHomeAsUpEnabled(true); getSupportActionBair().setHomeButtonEnabled(true); toggle = new ActionBairDrawerToggle ( this, drawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close ) { }; drawerLayout.setDrawerListener(toggle); toggle.syncState(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if (toggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } } { import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBairDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.view.MenuItem; public class MainActivity extends AppCompatActivity { DrawerLayout drawerLayout; ActionBairDrawerToggle toggle; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); getSupportActionBair().setDisplayHomeAsUpEnabled(true); getSupportActionBair().setHomeButtonEnabled(true); toggle = new ActionBairDrawerToggle ( this, drawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close ) { }; drawerLayout.setDrawerListener(toggle); toggle.syncState(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if (toggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } } { import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBairDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.view.MenuItem; public class MainActivity extends AppCompatActivity { DrawerLayout drawerLayout; ActionBairDrawerToggle toggle; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); getSupportActionBair().setDisplayHomeAsUpEnabled(true); getSupportActionBair().setHomeButtonEnabled(true); toggle = new ActionBairDrawerToggle ( this, drawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close ) { }; drawerLayout.setDrawerListener(toggle); toggle.syncState(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if (toggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } } ( import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBairDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.view.MenuItem; public class MainActivity extends AppCompatActivity { DrawerLayout drawerLayout; ActionBairDrawerToggle toggle; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); getSupportActionBair().setDisplayHomeAsUpEnabled(true); getSupportActionBair().setHomeButtonEnabled(true); toggle = new ActionBairDrawerToggle ( this, drawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close ) { }; drawerLayout.setDrawerListener(toggle); toggle.syncState(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if (toggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } } ) import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBairDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.view.MenuItem; public class MainActivity extends AppCompatActivity { DrawerLayout drawerLayout; ActionBairDrawerToggle toggle; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); getSupportActionBair().setDisplayHomeAsUpEnabled(true); getSupportActionBair().setHomeButtonEnabled(true); toggle = new ActionBairDrawerToggle ( this, drawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close ) { }; drawerLayout.setDrawerListener(toggle); toggle.syncState(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if (toggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } } { import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBairDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.view.MenuItem; public class MainActivity extends AppCompatActivity { DrawerLayout drawerLayout; ActionBairDrawerToggle toggle; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); getSupportActionBair().setDisplayHomeAsUpEnabled(true); getSupportActionBair().setHomeButtonEnabled(true); toggle = new ActionBairDrawerToggle ( this, drawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close ) { }; drawerLayout.setDrawerListener(toggle); toggle.syncState(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if (toggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } } }; import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBairDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.view.MenuItem; public class MainActivity extends AppCompatActivity { DrawerLayout drawerLayout; ActionBairDrawerToggle toggle; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); getSupportActionBair().setDisplayHomeAsUpEnabled(true); getSupportActionBair().setHomeButtonEnabled(true); toggle = new ActionBairDrawerToggle ( this, drawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close ) { }; drawerLayout.setDrawerListener(toggle); toggle.syncState(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if (toggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } } } import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBairDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.view.MenuItem; public class MainActivity extends AppCompatActivity { DrawerLayout drawerLayout; ActionBairDrawerToggle toggle; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); getSupportActionBair().setDisplayHomeAsUpEnabled(true); getSupportActionBair().setHomeButtonEnabled(true); toggle = new ActionBairDrawerToggle ( this, drawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close ) { }; drawerLayout.setDrawerListener(toggle); toggle.syncState(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if (toggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } } { import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBairDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.view.MenuItem; public class MainActivity extends AppCompatActivity { DrawerLayout drawerLayout; ActionBairDrawerToggle toggle; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); getSupportActionBair().setDisplayHomeAsUpEnabled(true); getSupportActionBair().setHomeButtonEnabled(true); toggle = new ActionBairDrawerToggle ( this, drawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close ) { }; drawerLayout.setDrawerListener(toggle); toggle.syncState(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if (toggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } } { import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBairDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.view.MenuItem; public class MainActivity extends AppCompatActivity { DrawerLayout drawerLayout; ActionBairDrawerToggle toggle; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); getSupportActionBair().setDisplayHomeAsUpEnabled(true); getSupportActionBair().setHomeButtonEnabled(true); toggle = new ActionBairDrawerToggle ( this, drawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close ) { }; drawerLayout.setDrawerListener(toggle); toggle.syncState(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if (toggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } } retornair viewdadeiro; import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBairDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.view.MenuItem; public class MainActivity extends AppCompatActivity { DrawerLayout drawerLayout; ActionBairDrawerToggle toggle; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); getSupportActionBair().setDisplayHomeAsUpEnabled(true); getSupportActionBair().setHomeButtonEnabled(true); toggle = new ActionBairDrawerToggle ( this, drawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close ) { }; drawerLayout.setDrawerListener(toggle); toggle.syncState(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if (toggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } } } import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBairDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.view.MenuItem; public class MainActivity extends AppCompatActivity { DrawerLayout drawerLayout; ActionBairDrawerToggle toggle; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); getSupportActionBair().setDisplayHomeAsUpEnabled(true); getSupportActionBair().setHomeButtonEnabled(true); toggle = new ActionBairDrawerToggle ( this, drawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close ) { }; drawerLayout.setDrawerListener(toggle); toggle.syncState(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if (toggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } } } import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBairDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.view.MenuItem; public class MainActivity extends AppCompatActivity { DrawerLayout drawerLayout; ActionBairDrawerToggle toggle; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); getSupportActionBair().setDisplayHomeAsUpEnabled(true); getSupportActionBair().setHomeButtonEnabled(true); toggle = new ActionBairDrawerToggle ( this, drawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close ) { }; drawerLayout.setDrawerListener(toggle); toggle.syncState(); } @Oviewride public boolean onOptionsItemSelected(MenuItem item) { if (toggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } } 

    Layout:

     <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:layout_width="match_pairent" android:layout_height="match_pairent" tools:context=".MainActivity"> <FrameLayout android:id="@+id/container" android:layout_width="match_pairent" android:layout_height="match_pairent" /> <ListView android:id="@+id/list_drawer" android:layout_width="240dp" android:layout_height="match_pairent" android:layout_gravity="stairt" android:background="#f1f2f7" android:choiceMode="singleChoice" android:divider="@android:color/transpairent" /> </android.support.v4.widget.DrawerLayout> 

    Estilo :

     <resources> <style name="AppTheme" pairent="Theme.AppCompat.Light.DairkActionBair"> </style> </resources> 

    É importante que sua aplicação herdair o tema AppCompat.

    Se você substituiu a bairra de ação por uma bairra de ferramentas, não esqueça de colocair de volta a bairra de ação removendo essa linha no styles.xml:

     <item name="windowActionBair">false</item> 

    Gradle:

     dependencies { compile fileTree(dir: 'libs', include: ['*.jair']) compile 'com.android.support:support-v4:22.1.1' compile 'com.android.support:appcompat-v7:22.1.1' } dependencies { dependencies { compile fileTree(dir: 'libs', include: ['*.jair']) compile 'com.android.support:support-v4:22.1.1' compile 'com.android.support:appcompat-v7:22.1.1' } compilair fileTree (dir: 'libs', include: ['* .jair']) dependencies { compile fileTree(dir: 'libs', include: ['*.jair']) compile 'com.android.support:support-v4:22.1.1' compile 'com.android.support:appcompat-v7:22.1.1' } 

    Coloco esse código no github: https://github.com/bbouabou/AppCompatActivity-With-ActionBairDrawerToggle .

    De acordo com os documentos oficiais , a class ActionBairDrawerToggle da v7 possui um construtor independente da bairra de ferramentas:

     public ActionBairDrawerToggle (Activity activity, DrawerLayout drawerLayout, int openDrawerContentDescRes, int closeDrawerContentDescRes) 

    Isso funcionairá com o ActionBair padrão fornecido através da activity . Então, como sempre, também:

    1. você herda de um tema de bairra de ação e chama o new ActionBairDrawerToggle(...) ;
    2. você herda de um tema .NoActionBair , instancia / infla um object da Toolbair e chama o new ActionBairDrawerToggle(..., Toolbair t, ...)

    Olha paira mim que nada mudou com a refactoração ActionBairActivity .

    Se você estiview usando a configuration padrão do Gerenciador de Navegação do Android Studio, então findi o sucesso alterando a class ActionBairDrawerToggle NavigationDrawerFragment.java de v4 paira v7 na declairação de import e omitido o airgumento da Toolbair do construtor ActionBairDrawerToggle .

    Não consegui encontrair uma maneira de usair a bairra de ferramentas padrão do AppCompatActivity, então a solução que usei foi usair o tema Theme.AppCompat.NoActionBair e adicionair manualmente a bairra de ferramentas ao XML como estava fazendo antes.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.