问题描述
我试图让新TabLayout在android设计库的工作。
我下面这篇文章:
http://android-developers.blogspot.com/2015/05/android-design-support-library.html
和文档:
http://developer.android.com/reference/android/support/design/widget/TabLayout.html
和想出了下面的code在我的活动,但tablayout没有显示出来,当我跑活动。
我试图在活动的布局文件中添加,但它说,它无法找到该XML标记。
公共类TabActivity扩展BaseActivity {
SectionPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_tab);
的LinearLayout V =(的LinearLayout)findViewById(R.id.tabContainer);
TabLayout tabLayout =新TabLayout(本);
tabLayout.addTab(tabLayout.newTab()的setText(标签1));
tabLayout.addTab(tabLayout.newTab()的setText(标签2));
tabLayout.addTab(tabLayout.newTab()的setText(标签3));
tabLayout.setLayoutParams(新LinearLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,50));
v.addView(tabLayout);
mSectionsPagerAdapter =新SectionPagerAdapter(getFragmentManager());
mViewPager =(ViewPager)findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
tabLayout.setupWithViewPager(mViewPager);
mViewPager.addOnPageChangeListener(新TabLayout.TabLayoutOnPageChangeListener(tabLayout));
}
公共类SectionPagerAdapter扩展FragmentPagerAdapter {
私人字符串变量=SectionPagerAdapter;
公共SectionPagerAdapter(FragmentManager FM){
超(FM);
}
@覆盖
公共片段的getItem(INT位置)
{
返回新片段();
}
@覆盖
公众诠释getCount将(){
返回2;
}
@覆盖
公共CharSequence的getPageTitle(INT位置){
区域设置L = Locale.getDefault();
开关(位置){
情况下0:
返回测试;
情况1:
返回测试;
案例2:
}
返回null;
}
}
}
增加了以下我的摇篮文件
编译com.android.support:design:22.2.0
我刚刚成功地建立新的TabLayout,所以这里的快速步骤来做到这一点(ノ◕ヮ◕)ノ*:·゚✧
-
添加依赖您build.gradle文件中:
相关性{ 编译com.android.support:design:22.2.0 }
-
添加TabLayout您的布局中
< XML版本=1.0编码=UTF-8&GT?; < LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android 机器人:layout_width =match_parent 机器人:layout_height =match_parent 机器人:方向=垂直> < android.support.v7.widget.Toolbar 机器人:ID =@ + ID /工具栏 机器人:layout_width =match_parent 机器人:layout_height =WRAP_CONTENT 机器人:后台= />中ATTR / colorPrimary? < android.support.design.widget.TabLayout 机器人:ID =@ + ID / tab_layout 机器人:layout_width =match_parent 机器人:layout_height =WRAP_CONTENT/> < android.support.v4.view.ViewPager 机器人:ID =@ + ID /寻呼机 机器人:layout_width =match_parent 机器人:layout_height =match_parent/> < / LinearLayout中>
-
设置你的活动是这样的:
进口android.os.Bundle; 进口android.support.design.widget.TabLayout; 进口android.support.v4.app.Fragment; 进口android.support.v4.app.FragmentManager; 进口android.support.v4.app.FragmentPagerAdapter; 进口android.support.v4.view.ViewPager; 进口android.support.v7.app.AppCompatActivity; 进口android.support.v7.widget.Toolbar; 公共类TabLayoutActivity扩展AppCompatActivity { @覆盖 保护无效的onCreate(包savedInstanceState){ super.onCreate(savedInstanceState); 的setContentView(R.layout.activity_pull_to_refresh); 工具条工具栏=(栏)findViewById(R.id.toolbar); TabLayout tabLayout =(TabLayout)findViewById(R.id.tab_layout); ViewPager viewPager =(ViewPager)findViewById(R.id.pager); 如果(工具栏!= NULL){ setSupportActionBar(工具栏); } viewPager.setAdapter(新SectionPagerAdapter(getSupportFragmentManager())); tabLayout.setupWithViewPager(viewPager); } 公共类SectionPagerAdapter扩展FragmentPagerAdapter { 公共SectionPagerAdapter(FragmentManager FM){ 超(FM); } @覆盖 公共片段的getItem(INT位置){ 开关(位置){ 情况下0: 返回新FirstTabFragment(); 情况1: 默认: 返回新SecondTabFragment(); } } @覆盖 公众诠释getCount将(){ 返回2; } @覆盖 公共CharSequence的getPageTitle(INT位置){ 开关(位置){ 情况下0: 回到第一选项卡; 情况1: 默认: 回到第二个选项卡; } } } }
I'm trying to get the new TabLayout in the android design library working.
I'm following this post:
http://android-developers.blogspot.com/2015/05/android-design-support-library.html
and the documentation:
http://developer.android.com/reference/android/support/design/widget/TabLayout.html
And have come up with the following code in my activity but the tablayout isn't showing up when I run the activity.
I tried adding in the activity layout file, but it says it can't find that xml tag.
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;
}
}
}
Added the following to my gradle file
compile 'com.android.support:design:22.2.0'
I've just managed to setup new TabLayout, so here are the quick steps to do this (ノ◕ヮ◕)ノ*:・゚✧
Add dependencies inside your build.gradle file:
dependencies { compile 'com.android.support:design:22.2.0' }
Add TabLayout inside your layout
<?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>
Setup your Activity like this:
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"; } } } }
这篇关于安卓TabLayout Android的设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!