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

时间:2018-04-18 15:29:42

标签: android android-studio android-nestedscrollview

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

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

Screenshot of design tool

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

Screenshot of emulator

这是我的 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) {

}
}

2 个答案:

答案 0 :(得分:0)

    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>

在您的主要活动中添加此代码。

公共类MainActivity扩展了AppCompatActivity {

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

}

答案 1 :(得分:0)

您的问题是,YouTubeBaseActivity扩展了Activity而不是AppCompat,这是Google的框架框架中漂亮的决定。

不幸的是,如果没有为YouTube制作自己的活动,可以选择使用YouTubePlayerFragment(请参阅here),而不关心它所依赖的活动,所以你的活动现在可以是正常的AppCompat活动,你可以在片段中做你的YT内容。