安卓向上导航



Android破解可能会有所帮助吗,我在React Native应用程序上尝试在我的MainActivity上实现一个后退/关闭按钮.java用于向上导航。

(https://developer.android.com/.../implementing.../ancestral.html)

主活动.java - 不会编译。

import com.facebook.react.ReactActivity;
public class MainActivity extends ReactActivity {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    @Override 
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
        onBackPressed();
        return true;
        }
    }
    @Override
    protected String getMainComponentName() {
    return "myapp";
    }
}

编译错误:

Error:
:app:compileDebugJavaWithJavac
/Users/---/Desktop/myapp/android/app/src/main/java/com/myapp/MainActivity.java:12: error: invalid method declaration; return type required
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    ^
/Users/---/Desktop/myapp/android/app/src/main/java/com/myapp/MainActivity.java:12: error: ';' expected
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                         ^
2 errors
Incremental compilation of 1 classes completed in 0.624 secs.
:app:compileDebugJavaWithJavac FAILED
FAILURE: Build failed with an exception.

主应用.java

package com.myapp;
import android.app.Application;
import com.facebook.react.ReactApplication;
import com.audioStreaming.ReactNativeAudioStreamingPackage;
import com.inprogress.reactnativeyoutube.ReactNativeYouTube;
import com.oblador.vectoricons.VectorIconsPackage;
import com.pspdfkit.react.PSPDFKitPackage;
import com.geektime.rnonesignalandroid.ReactNativeOneSignalPackage;
import com.imagepicker.ImagePickerPackage;
import com.RNFetchBlob.RNFetchBlobPackage;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;
import java.util.Arrays;
import java.util.List;
public class MainApplication extends Application implements ReactApplication {
  private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
    @Override
    public boolean getUseDeveloperSupport() {
      return BuildConfig.DEBUG;
    }
    @Override
    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
          new MainReactPackage(),
            new ReactNativeAudioStreamingPackage(),
            new ReactNativeYouTube(),
            new VectorIconsPackage(),
            new PSPDFKitPackage(),
            new ReactNativeOneSignalPackage(),
            new ImagePickerPackage(),
            new RNFetchBlobPackage()
      );
    }
    @Override
    protected String getJSMainModuleName() {
      return "index";
    }
  };
  @Override
  public ReactNativeHost getReactNativeHost() {
    return mReactNativeHost;
  }
  @Override
  public void onCreate() {
    super.onCreate();
    SoLoader.init(this, /* native exopackage */ false);
  }
}

我的风格.xml

<resources>
  <style name="myAppTheme" parent="myAppTheme.Base"/>
   <style name="myAppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowBackground">@drawable/launch_screen</item>
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
  </style>
  <style name="myAppTheme.ExternalTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorWhite</item>
        <item name="colorPrimaryDark">@color/colorPrimary</item>
        <item name="colorAccent">@color/colorPrimary</item>
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="windowActionModeOverlay">true</item>
</style>
</resources>

所以大多数事情都是在 react-native 完成的,我不知道如何解决这个问题。这篇文章现在包括完整的MAinActiviy和MainApplication。

请确保您的样式.xml具有"黑暗行动栏"主题

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

使用代替

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar ">

将语句放在 onCreate 方法中

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

相关内容

  • 没有找到相关文章

最新更新