开发者

android tabHost

I have 2 questions regarding tabHost: I've created tabHost with 2 tabs and for the tab titles I use setIndicator(TextView) (I work with api level 4) my title background is white. I use selector for the title to choose between diff images for the title.

  1. I want to开发者_开发技巧 make the title text bold only when selected/pressed. I didn't succeed to do it using the selector I have. can I do it at all? the idea is that on cases I use drawable a I want the text bold. other cases not bold. same question regarding textColor.

  2. it looks like a bug - when the tab first opens, the text on the selected tab (the one I used in tabHost.setCurrentTab(tabId)) is not seen at all. after first press/focus/focus any other item it looks well. any idea why or how to solve this?

thanks in advance

on tabActivity -

TextView title1 = new TextView(MainActivity.getInstnace(), null, android.graphics.Typeface.NORMAL);
TextView title2 = new TextView(MainActivity.getInstnace(), null, android.graphics.Typeface.NORMAL);

title1.setText("teb11 title");
title1.setBackgroundResource(R.drawable.tabtitle);
title1.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.tab1), null, null, null);

title2.setText("tab22 title");
title2.setBackgroundResource(R.drawable.tabtitle);

title2.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.tab2), null, null, null);

TabSpec tab1 = mTabHost.newTabSpec("tab1").setIndicator(title1).setContent(R.id.list1);
TabSpec tab2 = mTabHost.newTabSpec("tab2").setIndicator(title2).setContent(R.id.list2);

mTabHost.addTab(tab1);
mTabHost.addTab(tab2);      
mTabHost.setCurrentTab(0);

the selector tab1.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">  
    <item android:state_selected="true"     
    android:drawable="@drawable/iconselect"/>   
    <item android:state_pressed="true"  
    android:drawable="@drawable/iconselect"/>   
    <item android:drawable="@drawable/icon"/>        
</selector> 

the selector for tabTitle

<selector xmlns:android="http://schemas.android.com/apk/res/android">  
    <item android:state_pressed="true"  
    android:drawable="@drawable/tabselected"/> 
    <item android:state_selected="true"     
    android:drawable="@drawable/tab" />  
    <item android:state_focused="true"  
    android:drawable="@drawable/tab" />  
</selector> 


For your question #1:
On your TabsAdapter use onPageSelected(int position) like this:

 public void onPageSelected(int position) {
      //your logic here
      fixTitleText();
 }

and:

 private void fixTitleText() {
      for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {
           View view = mTabHost.getTabWidget().getChildAt(i);
           TextView tv = (TextView) view.findViewById(android.R.id.title);
           tv.setTextColor(getResources().getColor(R.drawable.text_selector));
           tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
           tv.setTypeface(null, Typeface.BOLD);
      }
 }

just notice that this code is for HONEYCOMB and up, before this the tab host view hierarchy is a bit different


this is my 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="60px"
            android:background="@drawable/headerbk" 
            android:paddingTop="12px"
            android:orientation="vertical"
            android:gravity="center_horizontal">
        </TabWidget>
        <FrameLayout android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"              
            android:layout_height="fill_parent">
            <ListView android:id="@+id/list1" 
                android:background="@drawable/bk"
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent"
                android:layout_weight="1" 
                android:scrollbars="vertical"
                android:divider="@drawable/separationline"
                android:cacheColorHint="#00000000"  
                android:footerDividersEnabled="false"                   
                android:listSelector="@drawable/selected" />    
            <ListView android:id="@+id/list2" 
                android:background="@drawable/bk"
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent"
                android:layout_weight="1" 
                android:scrollbars="vertical"
                android:divider="@drawable/separationline" 
                android:cacheColorHint="#00000000" 
                android:footerDividersEnabled="false"
                android:listSelector="@drawable/selected" />                    
            </FrameLayout>
    </LinearLayout>
</TabHost>      


Can I see your layout XML or are you doing this only through Java commands. Either way, I'd like to see more than just some pseudocode to actually work on this.

That would be either your main.xml file or your OnCreate() function. IF you are setting your layout through XML, main.xml should have contents similar to :

<?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">
            <RelativeLayout android:id="@+id/RelativeLayout01"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:background="#ffffffff">
            </RelativeLayout>
            <RelativeLayout android:id="@+id/RelativeLayout02"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:background="#ffffffff">
            </RelativeLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>

It is possible to do it entirely through Java commands, but especially when fixing #2, we will likely need to see more of your code. As part of the OnCreate() function, I would recommend initializing all the stuff you would like to be shown. Otherwise, if it is not in your main.xml, and it is not initialized, it will not be shown.


If want the bold text on selection,

There may a different ways but what I did is...

to make custom TextView, and then Handle the touch event Like bold on action down and set to normal on action up.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜