其他分享
首页 > 其他分享> > Android标签自定义视图

Android标签自定义视图

作者:互联网

我使用以下代码向我的应用添加标签:

ActionBar.Tab tab1=actionBar.newTab();
tab1.setTabListener(this);
tab1.setCustomView(R.layout.tab_style);
TextView txt1 = (TextView)tab1.getCustomView().findViewById(R.id.tabtext);
txt1.setText(R.string.tab_1);
actionBar.addTab(tab1);

这是tab_style.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- This is the main layout of the application -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_basic_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white" 
    android:paddingBottom="15dp"
    >


<TextView
    android:id="@+id/tabtext"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
    android:paddingLeft="6dip"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:textSize="20dp"
/>
</RelativeLayout>

我想更改标签背景,但结果如下:

我该如何解决? Customview不是居中的.

解决方法:

“L”开发人员预览版中不推荐使用操作栏标签.

黑色区域是actionBar tabBar的背景.你必须设置actionBarTabBarStyle的样式并设置它的背景.看看这个链接:

http://blog.alwold.com/2013/08/28/styling-tabs-in-the-android-action-bar/

或者你可以使用其他图书馆:

PagerSlidingTabStrip

或者如果您想要谷歌,您可以尝试:

SlidingTabsBasic

标签:android,android-actionbar,android-custom-view,android-tabs
来源: https://codeday.me/bug/20190703/1363036.html