嵌套滚动视图隐藏了模拟器中的操作栏

Nested scroll view hides action bar in the emulator

我的 DescriptionActivity.java 使用 NestedScrollView 作为主容器。在设计视图中,我通常可以看到操作栏,但是当我打开模拟器时,操作栏被隐藏并且 activity 是全屏的。如何在模拟器中也显示操作栏?

这是我在设计视图中看到的:

这是我在模拟器中看到的:

这是我的xml代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:background="@drawable/background"
    tools:context="przemo.me.recommend.recommendme.DescriptionActivity">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:text="Trailer"
            android:textColor="@android:color/black"
            android:textSize="18sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            app:layout_constraintBottom_toTopOf="@+id/textView3"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView">

            <com.google.android.youtube.player.YouTubePlayerView
                android:id="@+id/youtubePlayerView"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

            </com.google.android.youtube.player.YouTubePlayerView>

        </FrameLayout>

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

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="przemo.me.recommend.recommendme">

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".CreatorsActivity" />
    <activity android:name=".DescriptionActivity"></activity>
</application>

样式:

<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">#a8a8a8</item>
    <item name="colorPrimaryDark">#000000</item>
    <item name="colorAccent">#000000</item>
</style>

DescriptionActvity.java

public class DescriptionActivity extends YouTubeBaseActivity
                        implements YouTubePlayer.OnInitializedListener {

TextView filmDesTextView;
YouTubePlayerView youTubePlayerView;
String GOOGLE_API_KEY = "AIzaSyAniT";
String videoId;

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

    setTitle("Description");

    filmDesTextView = (TextView) findViewById(R.id.filmDesTextView);
    youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtubePlayerView);

    AsyncHttpClient client = new AsyncHttpClient();

    Intent intent = getIntent();
    String title = intent.getStringExtra("title");

    client.get("https://tastedive.com/api/similar?q=" + title + "&k=movie&info=1", new JsonHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {

            try {

                String description = response.getJSONObject("Similar").getJSONArray("Info").getJSONObject(0).getString("wTeaser");
                final String url = response.getJSONObject("Similar").getJSONArray("Info").getJSONObject(0).getString("yUrl");
                final int REQUEST_CODE = 1;

                Pattern pattern = Pattern.compile("(?<=youtu.be/|watch\?v=|/videos/|embed\/)[^#\&\?]*");
                Matcher matcher = pattern.matcher(url);

                if (matcher.find()) {
                    videoId = matcher.group(0);
                }
                filmDesTextView.setText(description);

                youTubePlayerView.initialize(GOOGLE_API_KEY, new YouTubePlayer.OnInitializedListener() {
                    @Override
                    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {

                        if(!wasRestored) {
                            youTubePlayer.cueVideo(videoId, REQUEST_CODE);
                        }
                    }

                    @Override
                    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {

                    }
                });
            } catch (JSONException e) {

                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, Throwable e, JSONObject response) {

            Toast.makeText(DescriptionActivity.this, "Data loading failed!", Toast.LENGTH_SHORT).show();
        }
    });

}

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {


}

@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {

}
}
    Can you please update this style in your project !

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
      <!-- Customize your theme here. -->
      <item name="colorPrimary">@color/colorPrimary</item>
      <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
      <item name="colorAccent">@color/colorAccent</item>
    </style>

将此代码添加到您的主 activity。

public class MainActivity 扩展 AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getSupportActionBar().show();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

}

你的问题是 YouTubeBaseActivity 扩展了 Activity 而不是 AppCompat,这是 Google 的 漂亮 决定' s clusterfuck 的框架。

不幸的是,除了让您的 自己 activity 用于 YouTube,替代方法是使用 YouTubePlayerFragment(参见 here)不在乎 Activity 它靠什么生存,所以你的 activity 现在可以是一个普通的 AppCompat activity,你可以在 Fragment 中做 YT 的事情。