[英]Textview in Fragment not working
I am currently working on adding tabs into my activity. 我目前正在将标签添加到我的活动中。 For the same, I am using the Google SlidingTabLayout and SlidingTabStrip .
同样,我正在使用Google SlidingTabLayout和SlidingTabStrip 。
My fragment xml looks like this: 我的片段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>
My contents xml looks like this: 我的内容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>
and my code looks like this: 我的代码如下所示:
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;
}
}
My fragment adaptor looks like this: 我的片段适配器如下所示:
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;
}
}
and this is how i am invoking the tabs: 这就是我调用选项卡的方式:
mViewPager= (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(new TabPagerAdaptor(getSupportFragmentManager()));
mTabs = (SlidingTabLayout) findViewById(R.id.tabs);
mTabs.setViewPager(mViewPager);
But when I run the code, I can't find the textview being shown even though two tabs are working fine. 但是,当我运行代码时,即使两个选项卡都可以正常工作,我也找不到显示的textview。
You cannot view your textView because the height of the ViewPager is 0dp
in your xml - 您无法查看
0dp
,因为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"
/>
you have to change that to either wrap_content
or match_parent
- 您必须将其更改为
wrap_content
或match_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"
/>
Also, Your LinearLayout's orientation is horizontal
and the width of SlidingTabLayout
is match_parent
. 另外,LinearLayout的方向是
horizontal
,而SlidingTabLayout
的宽度是match_parent
。 SlidingTabLayout
is occupying whole screen width leaving no room to display Viewpager
. SlidingTabLayout
占据整个屏幕宽度,没有空间显示Viewpager
。
Your LinearLayout's orientation should be vertical not horizontal 您的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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.