【发布时间】:2016-07-11 09:45:51
【问题描述】:
我想将标签布局放在屏幕中间,类似于此图像的显示方式:
这正是我想要的,但我似乎遇到了麻烦。以下是我的尝试:
profile.xml
<?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:
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 片段时,我的屏幕如下所示:
【问题讨论】:
标签: android android-layout android-tabs android-tablayout