该应用程序有一个ActionBar,ActionBar具有导航选项卡,每个包含文本.除了文字之外,我还需要向标签页添加图标.
我已经使用ActionBar.Tab.setIcon(drawable)成功添加了图标,但该图标显示在选项卡文本的左侧.
(我使用ActionBarSherlock,但是解决方案可以在本机和ABS实现中工作)
解决方法
您可以为每个选项卡设置自定义视图.
https://gist.github.com/3167287
PropositionListActivity.java
import com.actionbarsherlock.app.ActionBar; import com.actionbarsherlock.app.ActionBar.Tab; import com.actionbarsherlock.app.SherlockActivity; import android.os.Bundle; import android.app.Activity; import android.graphics.Color; import android.graphics.drawable.Drawable; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; import android.widget.ImageView; import android.support.v4.app.FragmentTransaction; import android.support.v4.app.NavUtils; public class PropositionListActivity extends SherlockActivity implements ActionBar.TabListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.proposition_list); getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); createTab(R.layout.tab_to_opine,R.id.tab_to_opine_title,"Vou Opinar"); createTab(R.layout.tab_opined,R.id.tab_opined_title,"Já Opinei"); createTab(R.layout.tab_Feedback,R.id.tab_Feedback_title,"Feedback"); } public void createTab(int view,int titleView,String title) { ActionBar.Tab tab = getSupportActionBar().newTab(); tab.setTabListener(this); tab.setCustomView(view); getSupportActionBar().addTab(tab); ((TextView) findViewById(titleView)).setText(title); } }
tab_to_opine.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/tab_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/tab_to_opine" android:background="@android:color/transparent" /> <TextView android:id="@+id/tab_to_opine_title" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>