ViewPager缺少标题

时间:2016-09-09 10:33:42

标签: android android-viewpager

我想要像Google Play商店中的滑动页面一样。

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"

    tools:context="com.nielyouri.pluff.MainActivity">

    <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pager"
        android:layout_height="0px"
        android:layout_weight="1"
        android:layout_width="match_parent">

        <android.support.v4.view.PagerTitleStrip
            android:id="@+id/pager_title_strip"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:background="#33b5e5"
            android:textColor="#fff"
            android:paddingTop="4dp"
            android:paddingBottom="4dp" />

    </android.support.v4.view.ViewPager>
</LinearLayout>

MainActivity.java

package com.nielyouri.pluff;

import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class MainActivity extends FragmentActivity {

    //private static final String TAG = MainActivity.class.getSimpleName();

    private DayAdapter mDayAdapter;
    private ViewPager mViewPager = null;

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

        mDayAdapter = new DayAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mDayAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menu, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.about:
                Intent intent = new Intent(this, AboutActivity.class);

                startActivity(intent);

                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

enter image description here

但是,当我运行应用程序模拟时,它显示没有“Pluff”标题......

enter image description here

我的片段和适配器

http://pastebin.com/2utCxSZx

http://pastebin.com/eexzKpb6

尝试在互联网上发现的多件事情,改变xml等并没有办法解决问题。

1 个答案:

答案 0 :(得分:0)

在xml中,您必须在操作栏的工具栏代码下面添加:

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
    </android.support.v7.widget.Toolbar>

在java文件中:

一个。使用AppCompatActivity扩展类

湾你必须把下面的代码设置为标题:

Toolbar mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar);
        mActionBarToolbar.setTitle("Pluff");
        setSupportActionBar(mActionBarToolbar);

在清单文件中,您必须为活动设置主题,请参阅以下内容:

<activity
            android:name=".MainActivityPage"
            android:label="@string/app_name"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar"
            >

        </activity>
相关问题