首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将TabLayout放在屏幕中央

将TabLayout放在屏幕中央
EN

Stack Overflow用户
提问于 2016-07-11 17:45:52
回答 0查看 1.7K关注 0票数 1

我想将我的选项卡布局放在屏幕的中间,类似于此图像的显示方式:

这正是我想要的方式,但我似乎遇到了麻烦。下面是我的尝试:

profile.xml

代码语言:javascript
运行
复制
<?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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:fresco="http://schemas.android.com/tools"
    android:id="@+id/profile_layout"
    android:layout_height="match_parent">

    <com.facebook.drawee.view.SimpleDraweeView
        android:layout_marginTop="32dp"
        android:id="@+id/profile_picture"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_marginLeft="16dp"
        fresco:roundingBorderColor="@color/white"
        fresco:roundingBorderWidth="10dp"
        fresco:placeholderImageScaleType="centerCrop"
        fresco:placeholderImage="@mipmap/blank_prof_pic"
        fresco:roundAsCircle="true"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="32dp"
        android:layout_toRightOf="@id/profile_picture"
        android:textStyle="bold"
        android:id="@+id/profile_name"/>

    <Button
        android:stateListAnimator="@null"
        android:layout_marginLeft="64dp"
        android:layout_below="@id/profile_picture"
        android:text="@string/followers"
        android:background="?android:attr/selectableItemBackground"
        android:id="@+id/followers_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:stateListAnimator="@null"
        android:layout_marginLeft="72dp"
        android:layout_below="@id/profile_picture"
        android:text="@string/following"
        android:background="?android:attr/selectableItemBackground"
        android:layout_toRightOf="@+id/followers_button"
        android:id="@+id/following_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


    <com.cengalabs.flatui.views.FlatButton
        android:id="@+id/follow_button"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="16dp"
        android:fontFamily="sans-serif-medium"
        android:textSize="12sp"
        android:layout_width="wrap_content"
        android:layout_height="36dp"
        android:paddingLeft="24dp"
        android:paddingRight="24dp"
        android:visibility="gone"
        android:textColor="@color/white"
        android:layout_toRightOf="@id/profile_picture"
        android:layout_below="@id/profile_name"
        android:layout_marginBottom="16dp"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/comments_post_tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_below="@+id/app_bar"
        android:background="?attr/colorPrimary"
        android:elevation="6dp"
        app:tabIndicatorColor="#00BCD4"
        android:minHeight="60dp"
        app:tabGravity="fill"
        app:tabMode="fixed"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

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

</RelativeLayout>

Profile.java:

代码语言:javascript
运行
复制
public class ProfileTab extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    fragmentView = inflater.inflate(R.layout.profile, container, false);
    setupPostsAndCommentsTabLayout(fragmentView);
    ...
    }
    private void setupPostsAndCommentsTabLayout(View fragmentView) {
        tabLayout = (TabLayout) fragmentView.findViewById(R.id.comments_post_tab_layout);
        tabLayout.addTab(tabLayout.newTab().setText("Posts"));
        tabLayout.addTab(tabLayout.newTab().setText("Comments"));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        final ViewPager viewPager = (ViewPager) fragmentView.findViewById(R.id.comments_posts_pager);
        viewPager.setOffscreenPageLimit(2);
        final PagerAdapter adapter = new PagerAdapter
            (getFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
}

然而,我尝试的问题是,尽管我试图在布局中设置TabLayout,但android:gravity=center被放在布局的顶部,而不是中间。此外,似乎出现了4个选项卡,而不仅仅是2个,我也对此感到困惑,因为我不知道在哪里或如何创建2个以上的选项卡。最后,由于某些原因,我的应用程序在一段时间后突然崩溃。你知道我可能会出错的地方吗?谢谢!下面是我导航到Profile片段时的屏幕:

EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38303821

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档