android - 如何在tablayout android中设置带有图标的文本

标签 android android-fragments android-tablayout

我想在我的选项卡 fragment 中同时显示图标和文本,我在我的代码中尝试了一些如下所示的操作,但它只显示文本。请有人帮助我,因为我是 fragment 的新手。 这是我的 Java 代码:

public class DashboardFragment extends Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 3 ;
private String tabTitles[] = new String[] { "Schedule", "Payments", "Judgements" };
private int[] imageResId = {
        R.drawable.schedule_active,
        R.drawable.payment_summary_active,
        R.drawable.judgements_active
};


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    /**
     *Inflate tab_layout and setup Views.
     */
    if (container != null) {
        container.removeAllViews();
    }
    View x =  inflater.inflate(R.layout.tab_layoutdash,null);
    tabLayout = (TabLayout) x.findViewById(R.id.tabs);
    viewPager = (ViewPager) x.findViewById(R.id.viewpager);
    tabLayout.setSelectedTabIndicatorHeight(0);

    Toolbar toolbar = (Toolbar) x.findViewById(R.id.toolbar);
 toolbar.setTitle("                Dashboard");
    toolbar.setTitleTextColor(Color.WHITE);


    /**
     *Set an Apater for the View Pager
     */
    viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));

    /**
     * Now , this is a workaround ,
     * The setupWithViewPager dose't works without the runnable .
     * Maybe a Support Library Bug .
     */

    tabLayout.post(new Runnable() {
        @Override
        public void run() {
            tabLayout.setupWithViewPager(viewPager);

        }
    });
   createTabIcons();
    return x;

}


class MyAdapter extends FragmentPagerAdapter {

    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    /**
     * Return fragment with respect to Position .
     */

    @Override
    public Fragment getItem(int position)
    {
        switch (position){
            case 0 : return new Schedule();
            case 1 : return new Payment();
            case 2 : return new Judgement();
        }
        return null;
    }

    @Override
    public int getCount() {

        return int_items;

    }

    /**
     * This method returns the title of the tab according to the position.
     */

    @Override
    public CharSequence getPageTitle(int position) {

     Drawable image =  getView().getResources().getDrawable(imageResId[position]);
        image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
        // Replace blank spaces with image icon
        SpannableString sb = new SpannableString(" " + tabTitles[position]);
        ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
        sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return sb;


    }
}


}

这是 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_marginTop="60dp"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content"
>
<android.support.v7.widget.Toolbar
    android:layout_marginLeft="40dp"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimaryDark"/>

<android.support.design.widget.TabLayout
    android:theme="@style/tab"
    android:tabStripEnabled="false"
    android:id="@+id/tabs"
    app:tabGravity="fill"
    app:tabMode="fixed"
    android:background="#343434"
    app:tabSelectedTextColor="#ffffff"
    app:tabTextColor="@color/white"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

</android.support.design.widget.TabLayout>

<android.support.v4.view.ViewPager

    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</android.support.v4.view.ViewPager>


</LinearLayout>

最佳答案

Best TabLayout Example 找到最好的例子

以上示例包含所有类型的标签布局,例如带图像的标签、固定标签等。

如果对你有帮助,请告诉我。

谢谢!

关于android - 如何在tablayout android中设置带有图标的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40882744/

相关文章:

android - 使用已安装的文件编辑器(如果有)编辑纯文本文件的 Intent

Android 慢速文件下载与 iOS 和黑莓 - DefaultHttpClient

android - FragmentManager.executePendingTransactions 不会立即调用 onCreateView()

java - 如何在 OnClickListener 中切换选项卡位置或替换选项卡位置

android - 如何在屏幕底部设置android TabLayout?

android - 以编程方式将 TabLayout 中的选项卡文本颜色更改为不同的颜色

android - android NSD onResolveFailed() 的错误代码意味着什么

java - 无法使用phonegap-plugin-push、cordova-plugin-crosswalk-webview 和 cordova-plugin-health 进行cordova android 构建

android - DialogFragment 的简单示例 - 示例代码不适合我

android - 在 fragment 之间传递序列化对象与 JSON