Android Action Bar Tabs, Styling the Icon and Text together

后端 未结 2 1685
萌比男神i
萌比男神i 2020-12-09 23:11

Firstly, there is the image of my current tab bar \"enter

What I want is either aligni

相关标签:
2条回答
  • 2020-12-09 23:54

    I managed to do it successfully. Here is the xml I used:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:p1="http://schemas.android.com/apk/res/android"
        p1:minWidth="15px"
        p1:minHeight="15px"
        p1:layout_width="wrap_content"
        p1:layout_height="wrap_content"
        p1:id="@+id/tabRelative"
        p1:gravity="center">
        <ImageView
            p1:src="@drawable/discover_icon_nightlife"
            p1:layout_width="20dp"
            p1:layout_height="20dp"
            p1:id="@+id/tabImage"
            p1:layout_centerHorizontal="true"
            p1:scaleType="centerInside"
            p1:layout_marginTop="4dp"
            p1:paddingTop="1dp" />
        <TextView
            p1:text="Menu"
            p1:layout_width="wrap_content"
            p1:layout_height="wrap_content"
            p1:id="@+id/tabText"
            p1:layout_centerHorizontal="true"
            p1:layout_below="@+id/tabImage"
            p1:paddingTop="2dp" />
    </RelativeLayout>
    

    And I setted it as custom view to my ab like goober_nuts answer suggests.

    It wasn't as good as I wanted it to be. I would also want to change the height of the tabs but haven't managed to do it. Here is the picture of the last look:

    Last Look

    0 讨论(0)
  • 2020-12-10 00:11

    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);
    
            }
    
    0 讨论(0)
提交回复
热议问题