Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think I have a starting point for you but it is a little bit fiddly. Setting up the action bar with the following code you can set the tabs to use you custom look:</p> <pre><code> //...somewhere in oncreate ActionBar bar = getActionBar(); bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); ActionBar.Tab tab1 = bar.newTab(); TextView tv1 = createTab("Tab 1", R.drawable.firsttabdrawable); tab1.setCustomView(tv1); tab1.setTabListener(this); ActionBar.Tab tab2 = bar.newTab(); tab2.setCustomView(createTab("Tab 2", R.drawable.secondTabDrawable)); tab2.setTabListener(this); ActionBar.Tab tab3 = bar.newTab(); tab3.setCustomView(createTab("Tab 3", R.drawable.thirdTabDrawable)); tab3.setTabListener(this); ActionBar.Tab tab4 = bar.newTab(); tab4.setCustomView(createTab("Tab 4", R.drawable.fourthTabDrawable)); tab4.setTabListener(this); bar.addTab(tab1); bar.addTab(tab2); bar.addTab(tab3); bar.addTab(tab4); </code></pre> <p>createTab method as follows:</p> <pre><code> private TextView createTab(String titleText, int tabBackgroundDrawableId) { TextView tv = new TextView(this); //set caption and caption appearance tv.setTextAppearance(this, R.style.MyDefaultTabTextAppearance); tv.setText(titleText); //set appearance of tab tv.setBackgroundResource(tabBackgroundDrawableId); tv.setLayoutParams(new android.view.ViewGroup.LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT)); //Make sure all tabs have the same height tv.setMaxLines(2); tv.setMinLines(2); return tv; } </code></pre> <p>However the actionbar seems to add the tab view to a parent view that adds padding so for each one you need to remove it. I did this in the oncreate after the first code block like so:</p> <pre><code> View view = (View) tv1.getParent(); LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams)view.getLayoutParams(); view.setPadding(0, 0, 0, 0); view.setLayoutParams(lp); </code></pre> <p>You may find a more graceful/straightforward way of doing this but as a starting point I thought it was worth posting.</p> <p>Hope this helps.</p> <p>ADDED:</p> <p>It may also be worth pointing out that if you have a more complicated layout for your tab you can inflate a layout xml file in the createTab method instead. For example using the following xml (mytab.xml):</p> <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"&gt; &lt;ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tabIcon"/&gt; &lt;TextView style="@style/MyDefaultTabTextAppearance" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toRightOf="@+id/tabIcon" android:minLines="2" android:maxLines="2" android:id="@+id/tabText"/&gt; &lt;/RelativeLayout&gt; </code></pre> <p>createTab becomes:</p> <pre><code> private RelativeLayout createTab(String titleText, int tabBackgroundDrawableId) { RelativeLayout rl = (RelativeLayout)this.getLayoutInflater().inflate(R.layout.mytab, null, false); //set appearance of tab rl.setBackgroundResource(tabBackgroundDrawableId); TextView tv = (TextView)rl.findViewById(R.id.tabText); //set caption tv.setText(titleText); ImageView iv = (ImageView)rl.findViewById(R.id.tabIcon); iv.setImageResource(R.drawable.ic_launcher);//or supply unique drawable to method? return rl; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload