2017-05-10 11 views
0

私はTabLayoutをテキストとアイコン(互いに隣接して)で構築しようとしています。 しかし、アイコンの上にアイコンが表示されています。 アイコンを追加するには、これを実行しています:アイコンがアンドロイドのTablayoutのテキストの隣に表示されていません。

private void setupTabIcons() { 
    tabLayout.getTabAt(0).setIcon(R.drawable.email_id_icon); 
    tabLayout.getTabAt(1).setIcon(R.drawable.email_id_icon); 
} 

これは私のフルコードです。何が間違っているのか教えてください。

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_vets_listing); 
    viewPager = (ViewPager) findViewById(R.id.viewpager); 
    setupViewPager(viewPager); 

    tabLayout = (TabLayout) findViewById(R.id.tabs); 
    tabLayout.setupWithViewPager(viewPager); 
    setupTabIcons(); 
} 

private void setupTabIcons() { 
    tabLayout.getTabAt(0).setIcon(R.drawable.email_id_icon); 
    tabLayout.getTabAt(1).setIcon(R.drawable.email_id_icon); 
} 

private void setupViewPager(ViewPager viewPager) { 
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); 
    adapter.addFragment(new WalkInFragment(),"Walk in"); 
    adapter.addFragment(new HomeVisitFragment(),"Home Visit"); 

    viewPager.setAdapter(adapter); 
} 

class ViewPagerAdapter extends FragmentPagerAdapter { 
    private final List<Fragment> mFragmentList = new ArrayList<>(); 
    private final List<String> mFragmentTitleList = new ArrayList<>(); 

    public ViewPagerAdapter(FragmentManager manager) { 
     super(manager); 
    } 

    @Override 
    public Fragment getItem(int position) { 
     return mFragmentList.get(position); 
    } 

    @Override 
    public int getCount() { 
     return mFragmentList.size(); 
    } 

    public void addFragment(Fragment fragment, String title) { 
     mFragmentList.add(fragment); 
     mFragmentTitleList.add(title); 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     return mFragmentTitleList.get(position); 
    } 
} 
+0

てみてください次のリンク:http://stackoverflow.com/questions/34370081/how-to-set-icon-next-to-text-in-tablayout –

+0

それともあなたが同様にこれを試すことができます。http:/ /www.androidhive.info/2015/09/android-material-design-working-with-tabs/ –

+0

私はアンドロイドハイブのチュートリアルに従っています。しかし、結果は期待通りではなかった。 アイコンがテキストに隣接するのではなく、テキストの上に表示されました。 – rajat44

答えて

0

カスタムタブレイアウトを作成して、希望どおりにアイコンを配置できます。 これを行う方法のサンプルコードを示します。

custom_tab.xmlレイアウトファイルを作成します。

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/tab" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:textColor="@color/colorAccent" 
android:textSize="15sp" 
/> 
これはそれを行うだろうこの

private void setupTabIcons() { 
TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null); 
tabOne.setText("ONE"); 
tabOne.setCompoundDrawablesWithIntrinsicBounds(android.R.drawable.arrow_down_float, 0, 0, 0); 
tabLayout.getTabAt(0).setCustomView(tabOne); 

TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null); 
tabTwo.setText("TWO"); 
tabTwo.setCompoundDrawablesWithIntrinsicBounds(android.R.drawable.checkbox_on_background, 0, 0, 0); 
tabLayout.getTabAt(1).setCustomView(tabTwo); 

TextView tabThree = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null); 
tabThree.setText("THREE"); 
tabThree.setCompoundDrawablesWithIntrinsicBounds(android.R.drawable.arrow_up_float, 0, 0, 0); 
tabLayout.getTabAt(2).setCustomView(tabThree); 

}

にセットアップアイコン方法を変更し

。ここにカスタムタブレイアウトの実装に関するgithubのリンクがあります。 Tab with icons

関連する問題