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条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?