Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
473 views
in Technique[技术] by (71.8m points)

xamarin.android - Android Action Bar Tabs, Styling the Icon and Text together

Firstly, there is the image of my current tab bar enter image description here

What I want is either aligning the images to very left, while keeping the text centered or moving the images on top of the text centered.

Here is how I add the texts:

var tab = this.ActionBar.NewTab ();            
tab.SetText (tabText);
tab.SetIcon (iconResourceId);

Here is my relevant style.xml entries:

<style name="Theme.Discover" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <item name="android:actionBarTabStyle">@style/MyActionBarTabStyle</item>
    <item name="android:actionBarTabTextStyle">@style/MyActionBarTabText</item>
    <item name="android:actionMenuTextColor">#ffffff</item>
    <item name="android:windowBackground">@drawable/bg</item>
</style>

<style name="MyActionBarTabStyle" parent="@android:style/Widget.Holo.Light.ActionBar.TabView">
    <item name="android:background">@drawable/action_tab_selector</item>
</style> 

<!-- ActionBar tabs text styles -->
<style name="MyActionBarTabText"
       parent="@android:style/Widget.Holo.ActionBar.TabText">
    <item name="android:textColor">#ffffff</item>
</style>

I can understand java code too so if you are not familiar with Xamarin, I still appreciate the java examples&answers.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

My solution isn't perfect, but to move the icons above the text here is what I have so far, which might be able to help you.

TabLayout.axml

<?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:background="@android:color/transparent" />
    <TextView
        android:id="@+id/tab_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

MainActivity.cs

 void AddTabToActionBar(int labelResourceId, int iconResourceId)
        {
            var tab = this.ActionBar.NewTab();
            tab.SetCustomView(Resource.Layout.Tablayout);
            tab.CustomView.FindViewById<ImageView>(Resource.Id.tabImage).SetImageResource(iconResourceId);
            tab.CustomView.FindViewById<TextView>(Resource.Id.tabText).SetText(labelResourceId);
            tab.TabSelected += TabOnTabSelected;
            ActionBar.AddTab(tab);

        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...