AndroidのFragmentタブ画像をxmlのImageViewで指定

android fragment タブ カスタマイズ リサイズ
android fragment タブ カスタマイズ リサイズ

androidのfragmentでタブ画像をカスタマイズした時のメモ。

MainActivity.java


TabLayout mTabLayout = (TabLayout) findViewById(R.id.tab_layout); mTabLayout.setupWithViewPager(viewPager); mTabLayout.getTabAt(0).setText("").setCustomView(android.R.layout.tab_image_view_1); mTabLayout.getTabAt(1).setText("").setCustomView(android.R.layout.tab_image_view_2);

setCustomViewでレイアウトのxmlファイルを指定するのがポイント。

レイアウトは下記

res/layout/tab_image_view.xml


<ImageView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="80dp" android:layout_height="80dp" android:scaleType="centerInside" android:src="@drawable/hoge_image" />

hoge_imageが表示した画像ファイル

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください