我有一个使用Action Bar Tabs(使用ViewPager)的项目.在它们之间滑动时,标签移动非常顺畅,但我需要在TAB 2中添加两个子标签,然后移动到下一个标签或当然返回,就像在Glassdoor或Flipboard上一样.
请帮忙.
主要活动
public class MainActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }
    @Override
    public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return Tab1Fragment.newInstance();
                case 1:
                    return Tab2Fragment.newInstance();
                default:
                    return Tab3Fragment.newInstance(); …android android-viewpager android-tabs android-studio android-tabbed-activity
我正面临Android 5.0及以上版本的问题.每当我将任何视图对齐到父底部时,内容就会隐藏在软导航按钮后面.
以上附件是从Android Studio 1.5创建新项目后生成的屏幕截图.我刚刚在片段的底部添加了一个编辑文本.
如您所见,编辑文本的一半位于导航栏后面.
虽然代码是由Android Studio自动生成的,但我仍然会在下面发布代码.
的Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.trial.exps.tabbedtrial">
    <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    </application>
</manifest>
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.trial.exps.tabbedtrial.MainActivity">
    <android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/appbar_padding_top"
    android:theme="@style/AppTheme.AppBarOverlay">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay">
    </android.support.v7.widget.Toolbar>
    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    </android.support.design.widget.AppBarLayout>
    <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" />
    <android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom" …android navigationbar android-5.0-lollipop android-tabbed-activity
其他一些用户和我正在为Stack Exchange聊天网络开发Android应用程序.我们正在为每个活动添加一个教程,向用户解释UI.但是,我们遇到了一些障碍.
教程库需要一个指向视图的指针(无论是a TextView,ImageView等等),以便在显示中获取视图的坐标,以便知道在哪里绘制阴影和内容.
我们有一项活动使用Android Studio中的标准"标签式活动",因此我们不使用任何自定义工具栏.
操作栏如下所示:
我们想要抓住TextView每个标签上的指针,该标签包含标签的标题.
例如,我们希望能够访问Textview:
我们在互联网上找不到关于如何做到这一点的任何事情都没有真正成功.如果您使用自定义工具栏似乎相对容易,但我们不是.
在AOSP源代码中挖掘,我们发现了一种潜在的方法,但是我们需要访问的字段是private主活动代码中的其他字段,也可能是其中无法访问的字段.
所以问题是,我们怎样才能抓住指针TextView呢?它甚至可能吗?
我正在尝试在android中使用tabbedActivity来创建一个简单的应用程序.我想把我的标签放在屏幕的底部.
 这是我在使用android studio创建Activity时所拥有的.
这是我在使用android studio创建Activity时所拥有的.
我想要的是,"第1节第2节第3节"的部分位于屏幕的底部.
这是我的xml文件:
<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/appbar_padding_top"
    android:theme="@style/AppTheme.AppBarOverlay">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay">
    </android.support.v7.widget.Toolbar>
    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<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" />
这段代码包含在一个CoordinatorLayout.问题是,如果我TabLayout把它放在viewPager之后,它会从屏幕上删除.
有人有想法吗?
编辑:感谢@Vivek Mishra的回答,它的确有效!谢谢
我在Android Studio中创建了一个示例选项卡式活动,现在我想在其中显示一些我自己的片段,而不是默认的占位符文本.
在其他项目中,我将创建一个空白片段,并使用以下代码添加它:
BlankFragment1 blankFragment1 = new BlankFragment1();
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, blankFragment1).commit();
如果我导入空白片段类和xml布局并简单地在PlaceholderFragment类中粘贴这两行,我得到
Non-Static method cannot be referenced from a static context
所以,我尝试创建一个PlaceholderFragment实例并调用getSupportFragmentManager()它:
PlaceholderFragment placeholderFragment = new PlaceholderFragment();
        placeholderFragment.getSupportFragmentManager().beginTransaction().add(R.id.container, blankFragment1).commit();
但现在我明白了
Cannot resolve method 'getSupportFragmentManager()'
所以我认为可能有更好更简单的方法来做这件事.
在选项卡式活动中使用某些现有片段的最佳方法是什么?
作为参考,这是整个MainActivity类:
public class MainActivity extends FragmentActivity implements BlankFragment1.OnFragmentInteractionListener {
/**
 * The {@link android.support.v4.view.PagerAdapter} that will provide
 * fragments for each of the sections. We use a
 * {@link FragmentPagerAdapter} derivative, which will keep every
 * loaded fragment in memory. If this becomes …我正在向片段中的操作栏添加制表符.但是当我尝试启动该片段时,我的应用程序在抛出空指针异常后崩溃
这是错误
Home.java文件是这样的:
public class Home extends Fragment implements ActionBar.TabListener {
private ViewPager viewPager;
private TabPagerAdapter tabPagerAdapter;
private ActionBar actionBar;
private FragmentActivity fragmentActivity;
private String[] tabs ;
public Home() {
    // Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_home,container,false);
    fragmentActivity = new FragmentActivity();
    tabs = getResources().getStringArray(R.array.hometabs);
    viewPager = (ViewPager)rootView.findViewById(R.id.pager);
    actionBar = fragmentActivity.getActionBar();
    tabPagerAdapter = new TabPagerAdapter(fragmentActivity.getSupportFragmentManager());
    viewPager.setAdapter(tabPagerAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    for(String tab_name: tabs){ …