Java 片段中的Textview不工作
我目前正在将选项卡添加到我的活动中。出于同样的原因,我正在使用Google和 我的片段xml如下所示:Java 片段中的Textview不工作,java,android,android-fragments,Java,Android,Android Fragments,我目前正在将选项卡添加到我的活动中。出于同样的原因,我正在使用Google和 我的片段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"
<?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;
}
}
这就是我调用选项卡的方式:
mViewPager= (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(new TabPagerAdaptor(getSupportFragmentManager()));
mTabs = (SlidingTabLayout) findViewById(R.id.tabs);
mTabs.setViewPager(mViewPager);
但是当我运行代码时,我找不到显示的文本视图,即使两个选项卡工作正常 无法查看文本视图,因为在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"
/>
此外,线性布局的方向是水平的,而滑动布局的宽度是匹配父项的<代码>滑动布局占据了整个屏幕宽度,没有空间显示查看页面
线性布局的方向应该是垂直的,而不是水平的
<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>
您无法查看文本视图,因为在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"
/>
此外,线性布局的方向是水平的,而滑动布局的宽度是匹配父项的<代码>滑动布局占据了整个屏幕宽度,没有空间显示查看页面
线性布局的方向应该是垂直的,而不是水平的
<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>
您可以将代码发布到设置ViewPager适配器的位置吗?我已更新了代码,您可以将代码发布到设置ViewPager适配器的位置吗?我已更新了代码编辑了答案。我将其设置为水平,因为当我尝试将其设置为垂直时studio出现错误,但是现在它工作得很好。为你的支持干杯。编辑了答案。当我试图将其设置为垂直时,工作室出错,我将其设置为水平,但现在它工作得很好。为你的支持干杯