在叠加层(始终在顶部)窗口(Android)上创建标签视图

时间:2016-07-23 23:43:23

标签: java android

我正在尝试在叠加窗口上创建标签。或多或少在Facebook messenger android应用程序上完成。

这就是我所做的:

MainActivity.java

public class MainActivity extends AppCompatActivity
{

private overlay() {
        WindowManager mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                PixelFormat.TRANSLUCENT);
        params.gravity = Gravity.TOP | Gravity.LEFT;
        params.width = 800;
        params.height = 800;

        FrameLayout mFrameLayout = new FrameLayout(this);
        mWindowManager.addView(mFrameLayout, params);

        LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layoutInflater.inflate(R.layout.overlay_view, mFrameLayout);
        initTab(mFrameLayout);
}

private void initTab(FrameLayout mFrameLayout) {

        mViewPager = (ViewPager) mFrameLayout.findViewById(R.id.viewpager);
        mViewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(),
                MainActivity.this));

        mTabLayout = (TabLayout) mFrameLayout.findViewById(R.id.sliding_tabs);
        mTabLayout.setupWithViewPager(mViewPager);

}
}

MyFragmentPagerAdapter.java

public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
    final int PAGE_COUNT = 3;
    private String tabTitles[] = new String[]{"Tab1", "Tab2", "Tab3"};
    private Context context;

    public MyFragmentPagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.context = context;
    }

    @Override
    public int getCount() {
        return PAGE_COUNT;
    }

    @Override
    public Fragment getItem(int position) {
        return PublicChatFragment.newInstance(position + 1);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        // Generate title based on item position
        return tabTitles[position];
    }
}

overlay_view.xml

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


    <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        card_view:cardCornerRadius="10dp">


        <android.support.design.widget.TabLayout
            android:id="@+id/sliding_tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="scrollable" />

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="0px"
            android:layout_weight="1"
            android:background="@android:color/white" />


        <TextView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center" />

    </android.support.v7.widget.CardView>

</LinearLayout>

PublicChatFragment.java

public class PublicChatFragment extends Fragment {
    public static final String ARG_PAGE = "ARG_PAGE";

    private int mPage;


       public static PublicChatFragment newInstance(int page) {
            Bundle args = new Bundle();
            args.putInt(ARG_PAGE, page);
            PublicChatFragment fragment = new PublicChatFragment();
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mPage = getArguments().getInt(ARG_PAGE);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.public_chat_tab, null, false);
            TextView textView = (TextView) view;
            textView.setText("Fragment #" + mPage);
            return view;
        }
    }

public_chat_tab.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center" />

不幸的是,我遇到了这个错误:

java.lang.IllegalArgumentException: No view found for id 0x7f0f01a2 (com.example:id/viewpager) for fragment PublicChatFragment{3a7eb01 #1 id=0x7f0f01a2 android:switcher:2131689890:0}
      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1098)
      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
      at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)
      at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1632)
      at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:637)
      at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
      at android.support.v4.view.ViewPager.populate(ViewPager.java:1237)
      at android.support.v4.view.ViewPager.populate(ViewPager.java:1085)
      at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1611)
      at android.view.View.measure(View.java:18834)
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5953)
      at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
      at android.support.v7.widget.CardView.onMeasure(CardView.java:211)
      at android.view.View.measure(View.java:18834)
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5953)
      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
      at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
      at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
      at android.view.View.measure(View.java:18834)
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5953)
      at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
      at android.view.View.measure(View.java:18834)
      at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2131)
      at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1237)
      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1474)
      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1125)
      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6100)
      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
      at android.view.Choreographer.doCallbacks(Choreographer.java:670)
      at android.view.Choreographer.doFrame(Choreographer.java:606)
      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
      at android.os.Handler.handleCallback(Handler.java:739)
      at android.os.Handler.dispatchMessage(Handler.java:95)
      at android.os.Looper.loop(Looper.java:148)
      at android.app.ActivityThread.main(ActivityThread.java:5525)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)

我无法找到解决方案。谁能帮帮我?

更新

执行此行时会出现错误:

   mViewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(),
                    MainActivity.this));

0 个答案:

没有答案