[英]how to add icon on the top of text of actionbar in android
我剛接觸android。我正在嘗試實現一個具有三個選項卡的操作欄,每個選項卡包含一個圖標和選項卡名稱。我成功在每個選項卡上放置了圖標和文本,但不幸的是圖標在文本的左側(標簽名稱)中的標簽。 我想將圖標放在文本頂部而不是左側。 請找到我的代碼段,並幫助我找到解決方案。 提前致謝,
private void setActionBar()
{
ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayShowHomeEnabled(false);
bar.setDisplayShowTitleEnabled(false);
ActionBar.Tab tabA = bar.newTab().setText("TabA");
tabA.setIcon(R.drawable.iconA);
ActionBar.Tab tabB = bar.newTab().setText("TabB");
tabB.setIcon(R.drawable.iconB);
ActionBar.Tab tabC = bar.newTab().setText("TabC");
tabC.setIcon(R.drawable.iconC);
}
您可以使用自定義視圖來定義如何顯示選項卡。
這是一個粗略的示例:
自定義布局
<?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:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:id="@+id/tabIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingTop="2dp" />
<TextView
android:id="@+id/tabText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textColor="#FFFFFF" />
</LinearLayout>
膨脹視圖
View tabView = activity.getLayoutInflater().inflate(R.layout.actiobar_tab, null);
TextView tabText = (TextView) tabView.findViewById(R.id.tabText);
tabText.setText(R.String.sometext);
ImageView tabImage = (ImageView) tabView.findViewById(R.id.tabIcon);
tabImage.setImageDrawable(activity.getResources().getDrawable(R.drawable.someimage));
為給定標簽設置自定義視圖
Tab tab = actionBar.newTab().setCustomView(tabView)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.