myq_26 2012-12-17 02:42 采纳率: 16.7%
浏览 3574
已采纳

在Android中自定义Tabs

以下的activity是选项卡式的应用程序。

public class TabbedActivity extends TabActivity {

    /** activity第一次创建时调用 */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabbedactivity);        

        TabHost tabHost = getTabHost();         

        TabSpec photospec = tabHost.newTabSpec("RP");
        // 为Tab设置Title和Icon
        photospec.setIndicator("RP", getResources().getDrawable(R.drawable.tabrp));
        Intent photosIntent = new Intent(this, RP.class);
        photospec.setContent(photosIntent);

        // Tab for Songs
        TabSpec songspec = tabHost.newTabSpec("MP");
        songspec.setIndicator("MP", getResources().getDrawable(R.drawable.tabmp));
        Intent songsIntent = new Intent(this, MP.class);
        songspec.setContent(songsIntent);

        tabHost.addTab(photospec);
        tabHost.addTab(songspec);           
    }       
}

定义的tabbedactivity.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>
    </LinearLayout>
</TabHost>

我添加了tabmp.xml和tabrp.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:drawable="@color/white"/>
    <item android:state_focused="true" android:drawable="@color/white"/>
    <item android:state_pressed="true" android:drawable="@color/white"/>
    <item android:drawable="@color/gray" />
</selector>

我在color.xml中定义了颜色属性。程序也能很好的执行,但是当tabs有动作时,它的颜色是默认的黑色,没有动作时,是灰色的,当点击时是蓝色的。这样看来tabmp.xml和tabrp.xml是不能正常运行的,如何改这里的代码呢?

  • 写回答

2条回答 默认 最新

  • LiDaming_and 2012-12-19 05:33
    关注

    1.如果你创建了tab

     tabHost.getTabWidget().getChildAt(THE_CHILDS_POSITION_IN_THE_HOST).setBackgroundResource(R.drawable.tabmp.xml);
    

    2.为tab创建一个自定义的布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabsLayout" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:background="@drawable/tab_bg_selector"
    android:padding="10dp" android:gravity="center" android:orientation="vertical">
    
        <TextView android:id="@+id/tabsText" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="Title"
            android:textSize="15dp" android:textColor="@drawable/tab_text_selector"
            android:textStyle="bold" />
    </LinearLayout>
    

    然后创建你的tab:

    View tabview = LayoutInflator.from(context).inflate(R.layout.tabs_bg, null);
    TextView tv = (TextView)view.findViewById(R.id.tabsText);
    tv.setText(TEXT);
    TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(i);
    tabHost.addTab(setContent);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)