mirror of
https://github.com/zhigang1992/react-navigation.git
synced 2026-02-13 22:30:41 +08:00
Add support for customizing back button image. (#253)
For customizing back button image we use platform native functionality that is: `setBackIndicatorImage` on iOS and `setHomeAsUpIndicator` on Android. The reason we don't do that just by setting left item is that we get a couple of things for free such as handling RTL properly, working accessibility features, handling prop for hiding back button and a couple more. Unfortunately there are some downsides to that approach too. We need to install the back button as an Image component from the JS side, and the extract the image payload on the native side to set it with the navigator. This is specifically problematic in DEV mode where images are loaded asynchronously over HTTP from the packager. In order for that to work we had to employ a few hacks (more comments on that in the code).
This commit is contained in:
committed by
GitHub
parent
e61b8b3bd6
commit
0919404b2e
@@ -8,6 +8,7 @@ import android.view.Gravity;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.ViewParent;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.appcompat.app.ActionBar;
|
||||
@@ -15,6 +16,7 @@ import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
|
||||
import com.facebook.react.views.text.ReactFontManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -184,6 +186,17 @@ public class ScreenStackHeaderConfig extends ViewGroup {
|
||||
ScreenStackHeaderSubview view = mConfigSubviews.get(i);
|
||||
ScreenStackHeaderSubview.Type type = view.getType();
|
||||
|
||||
if (type == ScreenStackHeaderSubview.Type.BACK) {
|
||||
// we special case BACK button header config type as we don't add it as a view into toolbar
|
||||
// but instead just copy the drawable from imageview that's added as a first child to it.
|
||||
View firstChild = view.getChildAt(0);
|
||||
if (!(firstChild instanceof ImageView)) {
|
||||
throw new JSApplicationIllegalArgumentException("Back button header config view should have Image as first child");
|
||||
}
|
||||
actionBar.setHomeAsUpIndicator(((ImageView) firstChild).getDrawable());
|
||||
continue;
|
||||
}
|
||||
|
||||
Toolbar.LayoutParams params =
|
||||
new Toolbar.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user