gpt4 book ai didi

android - 在 Android 中的 TabLayout 中将图标添加到选项卡

转载 作者:搜寻专家 更新时间:2023-11-01 08:40:19 24 4
gpt4 key购买 nike

我想在 Android 的 TabLayout 中向我的选项卡添加图标。我尝试了图标数组和 getTitle 方法。我没有得到任何图标。选项卡仅保留标题文本,根本不使用标题文本数组中的文本。这是我的代码:

import statements...

public class scatter extends FragmentActivity {
some variable declarations...

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scatter);
sensorUID = getIntent().getStringExtra("sUID");
tankname = getIntent().getStringExtra("tNAME");
SCALE_C = (getIntent().getStringExtra("tSCALEC"));
SCALE_M = (getIntent().getStringExtra("tSCALEM"));
DPs = (getIntent().getStringExtra("tDP"));
UNITs = (getIntent().getStringExtra("tUNIT"));
numVar = Integer.parseInt(DPs);

final ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));

TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Graph"));
tabLayout.addTab(tabLayout.newTab().setText("Values"));

tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
pager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

@Override
public void onTabSelected(TabLayout.Tab tab) {
pager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}
});

}



private class MyPagerAdapter extends FragmentPagerAdapter {


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

@Override
public Fragment getItem(int pos) {
Fragment fragment;
Bundle args = new Bundle();
args.putString("senID", sensorUID);
args.putInt("numVar", numVar);

switch (pos) {
case 0:
fragment = FirstFragment.newInstance("FirstFragment, Instance 1");
break;
case 1:
fragment = SecondFragment.newInstance("SecondFragment, Instance 1");
break;
default:
fragment = FirstFragment.newInstance("FirstFragment, Instance 1");
break;
}
fragment.setArguments(args);
return fragment;
}


@Override
public int getCount() {
return 2;
}
}
}

最佳答案

TabLayout.Tab 有一个 setIcon() 方法。

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Graph").setIcon(R.drawable.ic_graph));
tabLayout.addTab(tabLayout.newTab().setText("Values").setIcon(R.drawable.ic_values));

关于android - 在 Android 中的 TabLayout 中将图标添加到选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33300714/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com