2016-12-04 1 views
1

앱을 만들고 있는데 조각에 뒤로 화살표가있는 작업 표시 줄을 넣으려고합니다.
그래서 이미 작업 표시 줄에 조각이 있지만 뒤로 화살표를 넣는 방법을 모릅니다. 제발 도와 주실 수 있습니까?
당신이 조각에서 뒤로 버튼 표시 할 경우 조각 라인을 따라
Guilherme
This is the fragment with an action bar tag뒤로 화살표가있는 작업 표시 줄

+0

당신이 styles.xml에서 "말할 때 http://stackoverflow.com/questions/13086840/actionbar-up-navigation-with-fragments – fbwnd

답변

3

추가 감사 : ((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);

그것은 더 나은 것을이이 일을 함께. 매니페스트 파일에 액티비티 부모를 추가하여 뒤로 화살표를 누를 때 부모 액티비티가 열려 있는지 확인합니다.

1

작업 표시 줄 대신 도구 모음의 뒤로 버튼에 대해 이와 같은 몇 가지 기능이 있습니다.

activity_main.xml에서 :

<RelativeLayout 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:id="@+id/activity_main" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.example.wolfmatrix.dummy.MainActivity"> 

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbarId" 
    android:layout_width="match_parent" 
    android:layout_height="48dp" 
    android:background="@color/colorPrimary"> 

    <TextView 
     android:id="@+id/toolbarTextId" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     android:textColor="@android:color/white" 
     android:textSize="14sp" /> 
</android.support.v7.widget.Toolbar> 

<ImageButton 
    android:id="@+id/backButtonIcon" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@color/colorPrimary" 
    android:gravity="left" 
    android:padding="10dp" 
    app:srcCompat="@drawable/ic_arrow_back_black_24dp" /> 

</RelativeLayout> 

styles.xml에서 :

<vector xmlns:android="http://schemas.android.com/apk/res/android" 
    android:width="24dp" 
    android:height="24dp" 
    android:viewportWidth="24.0" 
    android:viewportHeight="24.0"> 
<path 
    android:fillColor="#ffffff" 
    android:pathData="M20,11H7.83l5.59,-5.59L12,4l-8,8 8,8 1.41,-1.41L7.83,13H20v-2z"/> 

을 그리고 지금, 도구 모음 등을 가지고 사용하는 테마 =>Theme.AppCompat.Light.NoActionBar

ic_arrow_back_black_24dp.xml,이를 사용 아이콘도 있습니다. 코드 아래

+0

답변을 참조하십시오 : 테마를 사용 => Theme.AppCompat.Light.NoActionBar ", 새로운 스타일 태그를 만들어야합니다. –

+0

네,하지만'ActionBar' 대신'ToolBar'를 사용하고 싶다면,'Theme.AppCompat.Light.NoActionBar'로 바꿔야합니다. –

+0

그것은 다음과 같습니다 :' @ ' –

0

봅니다

는 OnBackStackChangedListener를 구현하고 조각 활동에이 코드를 추가합니다. 된 setContentView 후 라인 아래 추가

@Override 
public void onCreate(Bundle savedInstanceState) { 
    //Listen for changes in the back stack 
    getSupportFragmentManager().addOnBackStackChangedListener(this); 
    //Handle when activity is recreated like on orientation Change 
    shouldDisplayHomeUp(); 
} 

@Override 
public void onBackStackChanged() { 
    shouldDisplayHomeUp(); 
} 

public void shouldDisplayHomeUp(){ 
    //Enable Up button only if there are entries in the back stack 
    boolean canback = getSupportFragmentManager().getBackStackEntryCount()>0; 
    getSupportActionBar().setDisplayHomeAsUpEnabled(canback); 
} 

@Override 
public boolean onSupportNavigateUp() { 
    //This method is called when the up button is pressed. Just the pop back stack. 
    getSupportFragmentManager().popBackStack(); 
    return true; 
} 
0

상단

ActionBar actionBar = getSupportActionBar(); 
    if (actionBar != null) { 
     actionBar.setDisplayHomeAsUpEnabled(true); 
    } 

에에서 onCreate에 추가하고 활동

public boolean onOptionsItemSelected(MenuItem item) { 
    int id = item.getItemId(); 
    if (id == android.R.id.home) { 
     getSupportFragmentManager().popBackStack(); 
     finish(); 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

행운

0

에 기능을 추가()

//by doin that Back arrow will appear 
getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

onCreate() 이후에 다음과 같은 재정의 된 메서드를 만듭니다.

@Override 
    public boolean onSupportNavigateUp() { 
     finish(); 
     return super.onSupportNavigateUp(); 
    }