gpt4 book ai didi

Android TabLayout Android 设计

转载 作者:IT王子 更新时间:2023-10-29 00:03:20 27 4
gpt4 key购买 nike

我正在尝试让 android 设计库中的新 TabLayout 正常工作。

我正在关注这篇文章:

http://android-developers.blogspot.com/2015/05/android-design-support-library.html

和文档:

http://developer.android.com/reference/android/support/design/widget/TabLayout.html

并且在我的 Activity 中提出了以下代码,但是当我运行 Activity 时,tablayout 没有显示。

我尝试在 Activity 布局文件中添加,但它说找不到那个 xml 标签。

public class TabActivity extends BaseActivity {

SectionPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_tab);

LinearLayout v = (LinearLayout)findViewById(R.id.tabContainer);

TabLayout tabLayout = new TabLayout(this);
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));

tabLayout.setLayoutParams(new LinearLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, 50));

v.addView(tabLayout);

mSectionsPagerAdapter = new SectionPagerAdapter(getFragmentManager());

mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);

tabLayout.setupWithViewPager(mViewPager);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

}

public class SectionPagerAdapter extends FragmentPagerAdapter {

private String TAG = "SectionPagerAdapter";

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

@Override
public Fragment getItem(int position)
{
return new Fragment();
}

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

@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return "test";
case 1:
return "test";
case 2:
}
return null;
}
}
}

将以下内容添加到我的 gradle 文件中

 compile 'com.android.support:design:22.2.0'

最佳答案

我刚刚设法设置了新的 TabLayout,所以这里是执行此操作的快速步骤 (ノ◕ヮ◕)ノ*:・゚✧

  1. 在 build.gradle 文件中添加依赖项:

    dependencies {
    compile 'com.android.support:design:23.1.1'
    }
  2. 在布局中添加 TabLayout

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

    <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"/>

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

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

    </LinearLayout>
  3. 像这样设置您的 Activity :

    import android.os.Bundle;
    import android.support.design.widget.TabLayout;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentPagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;

    public class TabLayoutActivity extends AppCompatActivity {

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    ViewPager viewPager = (ViewPager) findViewById(R.id.pager);

    if (toolbar != null) {
    setSupportActionBar(toolbar);
    }

    viewPager.setAdapter(new SectionPagerAdapter(getSupportFragmentManager()));
    tabLayout.setupWithViewPager(viewPager);
    }

    public class SectionPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
    switch (position) {
    case 0:
    return new FirstTabFragment();
    case 1:
    default:
    return new SecondTabFragment();
    }
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
    switch (position) {
    case 0:
    return "First Tab";
    case 1:
    default:
    return "Second Tab";
    }
    }
    }

    }

关于Android TabLayout Android 设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30539772/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com