java - fragment 中的 TextView 不起作用

标签 java android android-fragments

我目前正在努力将选项卡添加到我的 Activity 中。同样,我正在使用 Google SlidingTabLayoutSlidingTabStrip

我的 fragment xml 如下所示:

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/textViewTab" />


</RelativeLayout>

我的内容 xml 如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.musicisfun.allaboutfootball.MainActivity"
    tools:showIn="@layout/activity_main">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <com.example.musicisfun.allaboutfootball.tabs.SlidingTabLayout
        android:layout_width="match_parent"
        android:id="@+id/tabs"
        android:layout_height="wrap_content">
    </com.example.musicisfun.allaboutfootball.tabs.SlidingTabLayout>

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

</LinearLayout>

</RelativeLayout>

我的代码如下所示:

 public static class TabFragment extends Fragment{

        public static TabFragment getInstance(int position){
            TabFragment tabFragment = new TabFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("site",position);
            tabFragment.setArguments(bundle);
            return tabFragment;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View layout = inflater.inflate(R.layout.fragment_tab,container,false);
            TextView textViewTab = (TextView) layout.findViewById(R.id.textViewTab);
            Bundle bundle = getArguments();
            if (bundle!=null){
                textViewTab.setText("Current Tab is" + bundle.getInt("site"));

            }
            return layout;
        }
    }

我的 fragment 适配器如下所示:

class TabPagerAdaptor extends FragmentPagerAdapter{

        String[] tab_names;
        public TabPagerAdaptor(FragmentManager fm) {
            super(fm);
            tab_names=getResources().getStringArray(R.array.feed_names);
        }

        @Override
        public Fragment getItem(int position) {
            TabFragment tabFragment = TabFragment.getInstance(position);
            return tabFragment;
        }

        @Override
        public CharSequence getPageTitle(int position) {

            return tab_names[position];
        }

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


    }

这就是我调用选项卡的方式:

   mViewPager= (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(new TabPagerAdaptor(getSupportFragmentManager()));
    mTabs = (SlidingTabLayout) findViewById(R.id.tabs);
    mTabs.setViewPager(mViewPager);

但是当我运行代码时,即使两个选项卡工作正常,我也找不到显示的 TextView 。

最佳答案

您无法查看您的textView,因为ViewPager的高度为0dp在你的 xml 中 -

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

您必须将其更改为 wrap_contentmatch_parent -

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

     />

此外,您的 LinearLayout 的方向是 horizontalSlidingTabLayout 的宽度是 match_parentSlidingTabLayout占据整个屏幕宽度,没有空间显示 Viewpager

LinearLayout 的方向应该是垂直的而不是水平的

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <com.example.musicisfun.allaboutfootball.tabs.SlidingTabLayout
        android:layout_width="match_parent"
        android:id="@+id/tabs"
        android:layout_height="wrap_content">
    </com.example.musicisfun.allaboutfootball.tabs.SlidingTabLayout>

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

</LinearLayout>

关于java - fragment 中的 TextView 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36270532/

相关文章:

Android Studio - 无法解析 GoogleAuthUtil

android - boost 库: Android

android - BottomSheetDialogFragment未打开,仅显示小白条

java - Springboot嵌入mongo测试

已检查异常与未检查异常的 Java 示例

Java:如何在运行时使用可变参数正确构造正则表达式

android - 不同 fragment 的不同菜单

java - 如何防止多次安排 cron 任务

android - 在自动完成 TextView 的建议列表中找不到项目时显示消息

android - 如何在 fragment 中打开键盘