开发者

How to create curved or rounded tabs in Android

I want to show the curve at the bottom right side of my tabs. So how can it be done? Below is the code I have used for XML and Java files.

Code for first 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>     

Code for second XML

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->

<item android:drawable="@drawable/ic_tab_artists_white"
      android:state_selected="true" 
      android:state_pressed="false" />
<!-- When not selected, use white-->
<item android:drawable="@drawable/ic_tab_artists_grey" />
</selector>

Code for .java file

                Resources res = getResources(); 
                final TabHost MainTabHost = getTabHost();  
                TabHost.TabSpec spec; 
                Intent intent; 
                
                MainTabHost.getTabWidget().setStripEnabled(false);
                
                //call calendar Activity class
                intent = new Intent().setClass(this, CalendarForm.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                spec = MainTabHost.newTabSpec(res.getString(R.string.text_tabHost1)).setIndicator("Calendar",
                res.getDrawable(R.drawable.calendar_ic)).setContent(intent);
                
                MainTabHost.addTab(spec);
                
                //call History Activity class
                intent = new Intent().setClass(this, HistoryForm.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                spec = MainTabHost.newTabSpec(res.getString(R.string.text_tabHost2)).setIndicator("History",
                res.getDrawable(R.drawable.calendar_ic)).setContent(intent);
                MainTabHost.addTab(spec);
        
                //call Statistic Activity class
                intent = new Intent().setClass(this, StatisticForm.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                spec = MainTabHost.newTabSpec(res.getString(R.string.text_tabHost3)).setIndicator("Statistic",
                res.getDrawable(R.drawable.calendar_ic)).setContent(intent);
                MainTabHost.addTab(spec);
                
                //setbackground Style of tabHost开发者_JAVA技巧                                       
                MainTabHost.setCurrentTab(0); 
                MainTabHost.getTabWidget().setWeightSum(3);
                final TabWidget tabHost=getTabWidget();   
                MainTabHost.setBackgroundResource(R.drawable.back_image);
                
                for (int j = 0; j < MainTabHost.getTabWidget().getChildCount(); j++) 
                {
                    ((TextView)tabHost.getChildAt(j).findViewById(android.R.id.title)).setTextColor(Color.parseColor("#FFFFFF"));
                    ((TextView)tabHost.getChildAt(j).findViewById(android.R.id.title)).setTextSize(16);
                }   

How to create curved or rounded tabs in Android

This is what I got. Here, tabs are square. Now I want my tabs curve at the bottom right side


Check the question below u can get an idea.

how-to-put-some-finishing-touches-rounded-edges.

And also another solution is setting an image with round corners over the tab, I know, cheesy solution but works.

tab = tabs.newTabSpec("tab_Busquedas");
tab.setContent(new Intent().setClassName("com.grapp", "com.grapp.homes").putExtras(bundle));
tab.setIndicator(null,null);
tabs.addTab(tab);
//here you set the image with rounded corners over the tab.
tabs.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.mytab_roundedcorners);

R.drawable.mytab_roundedcorners will be a selector.

Edit:

Put the code

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
<corners android:bottomLeftRadius="0dp"
     android:bottomRightRadius="0dp" 
     android:topLeftRadius="5dp"
     android:topRightRadius="5dp" />
</shape>

In the xml file in the drawable folder. Then use the xml file as the background for the tab. It will be a rounded corner.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜