gpt4 book ai didi

java - Android Tablayout,为什么标签不移动?

转载 作者:搜寻专家 更新时间:2023-11-01 09:27:45 24 4
gpt4 key购买 nike

我正在学习本教程 https://www.youtube.com/watch?v=zcnT-3F-9JA我从 github 上使用了他的代码,但输出是错误的。我在顶部放了 3 个选项卡,当我按下选项卡时, Activity 应该会改变,但实际上什么也没有发生,屏幕上仍然只有我的 main_activity。希望有人会帮忙。这是我的代码

PS - 是的,每个 fragment 我也有 3 个 xml 文件(即使我也有 main_activity,但我必须找出问题所在,然后将 1tab 分配给主要 Activity )。对于这 3 个选项卡,我还有 3 个 java 文件。

XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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:background="#FFFFFF"
android:orientation="vertical"
tools:context=".MainActivity">

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />


<TextView
android:id="@+id/lvltext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/expa"
android:layout_centerInParent="true"
android:fontFamily="@font/futuracondensed"
android:text="@string/leveltext"
android:textColor="@color/black"
android:textSize="30sp" />

<TextView

android:id="@+id/lvlnum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/lvltext"
android:layout_centerInParent="true"
android:fontFamily="@font/futuracondensed"
android:text="@string/levelnum"
android:textColor="@color/black"
android:textSize="30sp" />

<ImageView
android:id="@+id/girl"
android:layout_width="wrap_content"
android:layout_height="262dp"
android:layout_below="@id/lvlnum"
android:src="@drawable/girl" />

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/girl"
android:fontFamily="@font/futuracondensed"
android:text="@string/button"
android:textColor="@color/black" />

<ImageView
android:id="@+id/girl2"
android:layout_width="match_parent"
android:layout_height="258dp"
android:layout_below="@id/button"
android:src="@drawable/fitnessmodel" />

<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/girl2"
android:fontFamily="@font/futuracondensed"
android:text="@string/button2"
android:textColor="@color/black" />

<LinearLayout
android:layout_below="@id/tabs"
android:id="@+id/expa"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">

<TextView
android:id="@+id/team_a_score"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="@font/futuracondensed"
android:gravity="center"
android:paddingBottom="5dp"
android:text="0"
android:textColor="@color/black"
android:textSize="60sp" />
</LinearLayout>

<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginTop="0dp"
android:background="@color/black" />

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">

<TextView
android:id="@+id/team_b_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/futuracondensed"
android:gravity="center"
android:paddingBottom="5dp"
android:text="100"
android:textColor="@color/black"
android:textSize="60sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>

</android.support.v4.widget.NestedScrollView>

Java

public class MainActivity extends AppCompatActivity {
private SectionsPageAdapter mSectionsPageAdapter;

private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mSectionsPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());

// Set up the ViewPager with the sections adapter.
mViewPager = findViewById(R.id.container);
setupViewPager(mViewPager);

TabLayout tabLayout = findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}

private void setupViewPager(ViewPager viewPager) {
SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
adapter.addFragment(new Tab1Fragment(), "TAB1");
adapter.addFragment(new Tab2Fragment(), "TAB2");
adapter.addFragment(new Tab3Fragment(), "TAB3");
viewPager.setAdapter(adapter);
}


@Override
public void onResume() {
super.onResume();
}
}



public class SectionsPageAdapter extends FragmentPagerAdapter {

private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();

public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}

public SectionsPageAdapter(FragmentManager fm) {
super(fm);
}

@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}

@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}

@Override
public int getCount() {
return mFragmentList.size();
}
}

最佳答案

使用你所遵循的库中给出的这个 XML,然后无论你想将它们添加到你已经采用的 fragment 布局中的任何设计。一切都会很好

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.chirag.slidingtabsusingviewpager.MainActivity">

<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toolbar"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SlidingTabsUsingViewPager"
android:textSize="20dp"/>


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

<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tablayout"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

</android.support.design.widget.TabLayout>

<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="@+id/pager">

</android.support.v4.view.ViewPager>

</LinearLayout>

关于java - Android Tablayout,为什么标签不移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49549089/

24 4 0