ViewPager选项卡的TextView没有进入中心

时间:2014-06-03 10:37:08

标签: android android-viewpager

TextView标签内的ViewPager's未进入标签的中心。

ViewPager的布局:

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager_action_type"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

在ViewPager中添加标签的代码:

 Tab newTab = actionBar.newTab().setTabListener(this);
    RelativeLayout tabView = (RelativeLayout) LinearLayout.inflate(this, R.layout.viewpager_tab_view, null);
    TextView tvTabTitle = (TextView) tabView.findViewById(R.id.tv_viewpager_tab_title);
    tvTabTitle.setText(mAppSectionsPagerAdapter.getPageTitle(i));
    newTab.setCustomView(tabView);
    actionBar.addTab(newTab);

标签的自定义布局:

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

         <TextView
            android:id="@+id/tv_viewpager_tab_title"
            android:layout_width="match_parent"
            android:layout_height="match_parent" android:layout_centerInParent="true"
            android:text="test"
            android:textColor="@android:color/white" />

    </RelativeLayout>   

以下是ViewPager标签的屏幕截图,仅启用了Android的ViewBounds: enter image description here

3 个答案:

答案 0 :(得分:2)

你可以用这个:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"   
     android:id="@+id/tv_viewpager_tab_title"
     android:layout_width="match_parent"
     android:layout_height="match_parent" 
     android:gravity="center"
     android:text="test"
     android:textColor="@android:color/white" />

并替换用于在ViewPager中添加Tab的代码:

Tab newTab = actionBar.newTab().setTabListener(this);
    TextView tvTabTitle = (TextView) LinearLayout.inflate(this, R.layout. viewpager_tab_view, null);
    tvTabTitle.setText(mAppSectionsPagerAdapter.getPageTitle(i));
    newTab.setCustomView(tabView);
    actionBar.addTab(newTab);

答案 1 :(得分:2)

标签的自定义布局更改为

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

         <TextView
            android:id="@+id/tv_viewpager_tab_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:text="test"
            android:textColor="@android:color/white" />

    </RelativeLayout>   

此链接可以帮助您Tab Views

答案 2 :(得分:0)

您可以使用ViewPagerIndicator插件自定义标签。

相关问题