在Xamarin Android中自定义Tab文本颜色,堆叠颜色和滑动功能?

时间:2017-09-06 13:50:31

标签: tabs xamarin.android

我在xamarin android中有三个标签,我使用了标签主机来创建这些标签。现在,我想更改这些选项卡中的文本颜色,我想使用滑动效果,就像Xamarin Forms中的选项卡页面一样。我怎样才能做到这一点?如果需要,请告诉我图书馆吗?

1 个答案:

答案 0 :(得分:1)

  

我想更改这些标签中的文字颜色,我想使用滑动效果,就像Xamarin表格中的标签页一样

你可以使用TabLayout,这样的用法:

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

   <android.support.design.widget.TabLayout
       android:layout_height="wrap_content"
       android:layout_width="match_parent"
       android:id="@+id/tablayout"
       app:tabTextColor="@color/colorPrimary"
       app:tabSelectedTextColor="@color/colorAccent"
       app:tabIndicatorColor="@android:color/holo_orange_light">

      <android.support.design.widget.TabItem
          android:text="tab 1"/>
      <android.support.design.widget.TabItem
          android:text="tab 2"/>
      <android.support.design.widget.TabItem
          android:text="tab 3"/>

   </android.support.design.widget.TabLayout>

 </LinearLayout>

更改文字颜色的属性:

 app:tabTextColor         -->   The default text color to be applied to tabs.
 app:tabSelectedTextColor -->   The text color to be applied to the currently selected tab. 
 app:tabIndicatorColor    -->   Color of the indicator used to show the currently selected tab. 

效果如this

  

使用滑动效果,就像Xamarin表格中的标签页一样

Xamarin.Forms中,标签与Page相关联,实际上当它在原生Android中呈现时,它与ViewPager相关联。因此,如果您想实现滑动效果功能,则必须自己编写代码。有关详细信息,请阅读document

编辑:

要使用TabLayout,您必须添加Xamarin.Android.Support.v4Xamarin.Android.Support.Design nuget包。

编辑2;

这是一个关于实现滑动效果的简单演示:

public class MainActivity : AppCompatActivity
{
    private List<Android.Support.V4.App.Fragment> mFragments = new List<Android.Support.V4.App.Fragment>();
    private List<string> mTabTitles = new List<string>();
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        SetContentView(Resource.Layout.Main);
        TabLayout tabLayout = FindViewById<TabLayout>(Resource.Id.tablayout);
        ViewPager viewPager = FindViewById<ViewPager>(Resource.Id.view_pager);

        initTab();
        mFragments.Add(new Fragment1());
        mFragments.Add(new Fragment1());
        mFragments.Add(new Fragment1());
        mFragments.Add(new Fragment1());

        viewPager.Adapter = new ViewPagerAdapter(SupportFragmentManager, mFragments,mTabTitles);
        tabLayout.SetupWithViewPager(viewPager);
    }

    private void initTab()
    {
        for (int i = 1; i < 5; i++)
        {
            mTabTitles.Add("Tab" + i);
        }
    }
}

public class ViewPagerAdapter : FragmentPagerAdapter
{
    private List<string> mTabTitles;
    private static  int FRAGMENT_COUNT = 4;

    public ViewPagerAdapter(Android.Support.V4.App.FragmentManager fragmentManager, List<Android.Support.V4.App.Fragment> fragments, List<string> tabTitles):base(fragmentManager)
    {
        mTabTitles = tabTitles;
    }

    public override Android.Support.V4.App.Fragment GetItem(int position)
    {
        Fragment1 testFragment = new Fragment1();
        return testFragment;
    }

    public override int Count => FRAGMENT_COUNT;

    public override ICharSequence GetPageTitleFormatted(int position)
    {
        return new Java.Lang.String(mTabTitles[position]);
    }
}

axml档案:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  <android.support.design.widget.TabLayout
      android:layout_height="wrap_content"
      android:layout_width="match_parent"
      android:id="@+id/tablayout"
    >
  </android.support.design.widget.TabLayout>
   <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
   </android.support.v4.view.ViewPager>

</LinearLayout>

Effect

编辑3

在我的演示中,Fragment应使用Android.Support.V4.App.Fragment,代码如下:

public class Fragment1 : Android.Support.V4.App.Fragment
{
    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Create your fragment here
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // Use this to return your custom view for this Fragment
        return inflater.Inflate(Resource.Layout.YourFragment, container, false);

        return base.OnCreateView(inflater, container, savedInstanceState);
    }
}