2016-11-20 60 views
-2

它發生在我的主要活動中,我調用了一個活動,我稱之爲collapsingToolbar的故事,以及一切工作正常只有我不能給回,因爲collapsinToolbar不生成按鈕背部。主要活動的按鈕返回collapsingToolbar - Android

Java代碼

package com.herprogramacion.alquileres; 

import android.database.Cursor; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v4.app.LoaderManager; 
import android.support.v4.content.CursorLoader; 
import android.support.v4.content.Loader; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.LinearLayoutManager; 
import android.support.v7.widget.RecyclerView; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 

import com.herprogramacion.alquileres.provider.Contrato.Alquileres; 

public class ActividadListaAlquileres extends AppCompatActivity implements  AdaptadorAlquileres.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor> { 

private RecyclerView listaUI; 
private LinearLayoutManager linearLayoutManager; 
private AdaptadorAlquileres adaptador; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.actividad_lista_alquileres); 
    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, "Filtro...", Snackbar.LENGTH_LONG) 
        .setAction("Acción", null).show(); 
     } 
    }); 

    // Preparar lista 


    listaUI = (RecyclerView) findViewById(R.id.lista); 


    listaUI.setHasFixedSize(true); 

    linearLayoutManager = new LinearLayoutManager(this); 


    listaUI.setLayoutManager(linearLayoutManager); 


    adaptador = new AdaptadorAlquileres(this, this); 


    listaUI.setAdapter(adaptador); 


    // Iniciar loader 
    getSupportLoaderManager().restartLoader(1, null, this); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.menu_actividad_lista_alquileres, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    int id = item.getItemId(); 

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

    return super.onOptionsItemSelected(item); 
} 

@Override 
public void onClick(AdaptadorAlquileres.ViewHolder holder, String idAlquiler) { 
    // Snackbar.make(findViewById(android.R.id.content), ":id = " + idAlquiler, 
    //  Snackbar.LENGTH_LONG).show(); 
} 


@Override 
public Loader<Cursor> onCreateLoader(int id, Bundle args) { 
    return new CursorLoader(this, Alquileres.URI_CONTENIDO, null, null, null, null); 
} 

@Override 
public void onLoadFinished(Loader<Cursor> loader, Cursor data) { 
    if (adaptador != null) { 
     adaptador.swapCursor(data); 
    } 
} 

@Override 
public void onLoaderReset(Loader<Cursor> loader) { 
} 
} 

,我產生了collapsingToolbar我的主要活動

package com.herprogramacion.alquileres; 

import android.content.Intent; 
import android.support.design.widget.CollapsingToolbarLayout; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.Toolbar; 

public class biografia2 extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_biografia2); 

    CollapsingToolbarLayout collapsingToolbar = 
      (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar); 
    collapsingToolbar.setTitle("Titulo "); 


} 
} 

XML代碼活動的Java代碼

在這裏我產生collapsingToolbar活動的3210

XML代碼

<?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" 
 
    android:id="@+id/main_content" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" 
 
    android:fitsSystemWindows="true"> 
 

 
    <android.support.design.widget.AppBarLayout 
 
     android:id="@+id/appbar" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="@dimen/detail_backdrop_height" 
 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
 
     android:fitsSystemWindows="true"> 
 

 
     <android.support.design.widget.CollapsingToolbarLayout 
 
      android:id="@+id/collapsing_toolbar" 
 
      android:layout_width="match_parent" 
 
      android:layout_height="match_parent" 
 
      app:layout_scrollFlags="scroll|exitUntilCollapsed" 
 
      android:fitsSystemWindows="true" 
 
      app:contentScrim="?attr/colorPrimary" 
 
      app:expandedTitleMarginStart="48dp" 
 
      app:expandedTitleMarginEnd="64dp" > 
 

 
      <ImageView 
 
       android:id="@+id/backdrop" 
 
       android:layout_width="match_parent" 
 
       android:layout_height="match_parent" 
 
       android:scaleType="centerCrop" 
 
       android:fitsSystemWindows="true" 
 
       app:layout_collapseMode="parallax" 
 
       android:src="@drawable/jack" 
 
       android:alpha="0.5"/> 
 
      <RelativeLayout 
 
       android:layout_width="match_parent" 
 
       android:layout_height="170dp" 
 
       android:layout_gravity="center"> 
 

 
      </RelativeLayout> 
 

 

 
      <android.support.v7.widget.Toolbar 
 
       android:id="@+id/toolbar" 
 
       android:layout_width="match_parent" 
 
       android:layout_height="?attr/actionBarSize" 
 
       app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
 
       app:layout_collapseMode="pin" /> 
 

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

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

 
    <android.support.v4.widget.NestedScrollView 
 
     android:layout_width="match_parent" 
 
     android:layout_height="match_parent" 
 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 
 

 
     <LinearLayout 
 
      android:layout_width="match_parent" 
 
      android:layout_height="match_parent" 
 
      android:orientation="vertical" 
 
      android:paddingTop="24dp"> 
 

 
      <android.support.v7.widget.CardView 
 
       android:layout_width="match_parent" 
 
       android:layout_height="wrap_content" 
 
       android:layout_margin="@dimen/card_margin"> 
 

 
       <LinearLayout 
 
        style="@style/Widget.CardContent" 
 
        android:layout_width="match_parent" 
 
        android:layout_height="wrap_content"> 
 

 
        <TextView 
 
         android:layout_width="match_parent" 
 
         android:layout_height="wrap_content" 
 
         android:text="Info" 
 
         android:textAppearance="@style/TextAppearance.AppCompat.Title" /> 
 

 
        <TextView 
 
         android:layout_width="match_parent" 
 
         android:layout_height="wrap_content" 
 
         android:text="@string/lorem_ipsum" /> 
 

 
       </LinearLayout> 
 

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

 
      <android.support.v7.widget.CardView 
 
       android:layout_width="match_parent" 
 
       android:layout_height="wrap_content" 
 
       android:layout_marginBottom="@dimen/card_margin" 
 
       android:layout_marginLeft="@dimen/card_margin" 
 
       android:layout_marginRight="@dimen/card_margin"> 
 

 
       <LinearLayout 
 
        style="@style/Widget.CardContent" 
 
        android:layout_width="match_parent" 
 
        android:layout_height="wrap_content"> 
 

 
        <TextView 
 
         android:layout_width="match_parent" 
 
         android:layout_height="wrap_content" 
 
         android:text="Friends" 
 
         android:textAppearance="@style/TextAppearance.AppCompat.Title" /> 
 

 
        <TextView 
 
         android:layout_width="match_parent" 
 
         android:layout_height="wrap_content" 
 
         android:text="@string/lorem_ipsum" /> 
 

 
       </LinearLayout> 
 

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

 
      <android.support.v7.widget.CardView 
 
       android:layout_width="match_parent" 
 
       android:layout_height="wrap_content" 
 
       android:layout_marginBottom="@dimen/card_margin" 
 
       android:layout_marginLeft="@dimen/card_margin" 
 
       android:layout_marginRight="@dimen/card_margin"> 
 

 
       <LinearLayout 
 
        style="@style/Widget.CardContent" 
 
        android:layout_width="match_parent" 
 
        android:layout_height="wrap_content"> 
 

 
        <TextView 
 
         android:layout_width="match_parent" 
 
         android:layout_height="wrap_content" 
 
         android:text="Related" 
 
         android:textAppearance="@style/TextAppearance.AppCompat.Title" /> 
 

 
        <TextView 
 
         android:layout_width="match_parent" 
 
         android:layout_height="wrap_content" 
 
         android:text="@string/lorem_ipsum" /> 
 

 
       </LinearLayout> 
 

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

 
     </LinearLayout> 
 

 
    </android.support.v4.widget.NestedScrollView> 
 

 
    <android.support.design.widget.FloatingActionButton 
 
     android:layout_height="wrap_content" 
 
     android:layout_width="wrap_content" 
 
     app:layout_anchor="@id/appbar" 
 
     app:layout_anchorGravity="bottom|right|end" 
 
     android:src="@drawable/ic_discuss" 
 
     android:layout_margin="@dimen/fab_margin" 
 
     android:clickable="true"/> 
 

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

回答

0

您應該能夠使用setDisplayHomeAsUpEnabled顯示默認的後退按鈕:

final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
setSupportActionBar(toolbar); 
getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
+0

有了這三行,在打開collapsintoolbar活動時,應用程序關閉 – Jorge1023

2

您可以使用app:navigationIcon屬性在工具欄組件的xml中添加後退導航圖標。

<android.support.v7.widget.Toolbar 
    android:id="@+id/materialup.toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:minHeight="?attr/actionBarSize" 
    app:layout_scrollFlags="scroll" 
    app:navigationIcon="?homeAsUpIndicator" />