Java 将选项卡布局从文字更改为图标
我仍然是永远学习代码,但需要一些帮助这一个 我想将选项卡视图中的文字更改为图标 我该怎么做 这是我的Main.javaJava 将选项卡布局从文字更改为图标,java,android,Java,Android,我仍然是永远学习代码,但需要一些帮助这一个 我想将选项卡视图中的文字更改为图标 我该怎么做 这是我的Main.java public class Main extends ActionBarActivity { Toolbar toolbar; ViewPager pager; ViewPagerAdapter adapter; SlidingTabLayout tabs; CharSequence Titles[] = {"Home", "Events
public class Main extends ActionBarActivity {
Toolbar toolbar;
ViewPager pager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;
CharSequence Titles[] = {"Home", "Events", "Bluetooth Chat"};
int Numboftabs = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setElevation(0);
// Creating The Toolbar and setting it as the Toolbar for the activity.
// Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles, Numboftabs);
// Assigning ViewPager View and setting the adapter.
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
// Assiging the Sliding Tab Layout View.
tabs = (SlidingTabLayout) findViewById(R.id.tabs);
tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width
// Setting Custom Color for the Scroll bar indicator of the Tab View
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.tabsScrollColor);
}
});
// Setting the ViewPager For the SlidingTabsLayout.
tabs.setViewPager(pager);
final Button b = new Button(Main.this);
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_pressed));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public void Main2(View view) {
startActivity(new Intent(this, Main2.class));
}
public void Bluetooth(View view) {
startActivity(new Intent(this, MainActivity.class));
}
@Override
public void onBackPressed() {
Main.this.finish();
}
}
这是我的ViewPagerAdapter.Java
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
CharSequence Titles[]; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is created
int NumbOfTabs; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created
// Build a Constructor and assign the passed Values to appropriate values in the class
public ViewPagerAdapter(FragmentManager fm,CharSequence mTitles[], int mNumbOfTabsumb) {
super(fm);
this.Titles = mTitles;
this.NumbOfTabs = mNumbOfTabsumb;
}
//This method return the fragment for the every position in the View Pager
@Override
public Fragment getItem(int i) {
switch (i) {
case 0:
return new Tab1();
case 1:
return new Tab2();
case 2:
return new Tab4();
}
return null;
}
// This method returns the titles for the Tabs in the Tab Strip
@Override
public CharSequence getPageTitle(int position) {
return Titles[position];
}
// This method returns the number of tabs for the tabs Strip
@Override
public int getCount() {
return NumbOfTabs;
}
}
非常感谢您的帮助。您可以使用ImageSpan&SpannableString显示图标而不是文本 首先创建一个可绘制源的数组
int tabIcons[] = {R.drawable.ic_tab_home,
R.drawable.ic_tab_job,
R.drawable.ic_tab_map,
R.drawable.ic_tab_comments
};
然后像这样重写getPageTitle()
@Override
public CharSequence getPageTitle(int position) {
Drawable drawable = getResources().getDrawable(tabIcons[position]);
drawable.setBounds(0, 0, 56, 56);
ImageSpan imageSpan = new ImageSpan(drawable);
SpannableString spannableString = new SpannableString(" ");
spannableString.setSpan(imageSpan, 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return spannableString;
}
当我输入以下代码时,我得到一个错误:错误:(50,43)错误:类管理器中的getDrawable方法不能应用于给定的类型;必需:未找到参数:int原因:实际参数列表和形式参数列表长度不同您是否在tabIcons[]数组中使用可绘制源id?确保您为选项卡放置了完全相同数量的可绘制标签。e、 g对于3个选项卡使用3个可绘制id。还要确保您的可绘制文件夹中存在所有可绘制文件。是的,如果我为getResources()创建了一个方法,我会出现上述错误,如果我没有创建方法,我会出现此错误错误错误:(48,29)错误:找不到符号方法getResources(),不要创建名为getResources()的方法,使用默认的getResources()。尝试getActivity().getResources().getDrawable(选项卡图标[position]);由于某些原因,它没有使用默认值,getResources()显示为红色,当我按Alt并输入时,如果我将代码放入Main.Java中,它会提供“创建Getter”、“创建属性”和“创建方法”getResources的选项,但在我的ViewPagerAdapter中它无法识别默认值。