Fragment中的Textview不起作用

时间:2016-03-28 20:23:37

标签: java android android-fragments

我目前正在为我的活动添加标签。同样,我使用的是Google SlidingTabLayoutSlidingTabStrip

我的片段xml看起来像这样:

def foo(ref10_data, another_var, yet_another_var):
    print(max(ref10_data))

def main():
    ...
    restore_dict = pickle.load(...)
    foo(**restore_dict)

我的内容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 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;
        }
    }

这就是我调用标签的方式:

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;
        }


    }

但是当我运行代码时,即使两个标签工作正常,我也找不到显示的textview。

1 个答案:

答案 0 :(得分:0)

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

<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>