setTitle 在安卓系统中无法正常工作

3

我创建了一个滑动标签活动,并试图动态更改应用栏的标题。但是无论我做什么,似乎都不起作用!这里是我的MainActivity.java:

public class MainActivity extends AppCompatActivity {


ViewPager mPager;
SlidingTabLayout mTabs;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    getSupportActionBar().setDisplayShowHomeEnabled(true);


    mPager = (ViewPager) findViewById(R.id.pager);

    mPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));

    mTabs = (SlidingTabLayout) findViewById(R.id.tabs);

    mTabs.setCustomTabView(R.layout.custom_tab_view, R.id.tabText);

    mTabs.setDistributeEvenly(true);

    mTabs.setSelectedIndicatorColors(getResources().getColor(R.color.colorAccent));
    mTabs.setBackgroundColor(getResources().getColor(R.color.colorPrimary));

    mTabs.setViewPager(mPager);

}

class MyPagerAdapter extends FragmentPagerAdapter {

    int tabIcons[] = {R.drawable.ic_events, R.drawable.ic_clients, R.drawable.ic_history};
    String[] tabText = getResources().getStringArray(R.array.tabs);

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

    @Override
    public Fragment getItem(int position) {

        switch (position){
            case 0:
                getSupportActionBar().setTitle("Events");
                EventsFragment eventsFragment = EventsFragment.getInstance(position);
                return eventsFragment;

            case 1:
                getSupportActionBar().setTitle("Clients");
                ClientsFragment clientsFragment = ClientsFragment.getInstance(position);
                return clientsFragment;

            case 2:
                getSupportActionBar().setTitle("History");
                HistoryFragment historyFragment = HistoryFragment.getInstance(position);
                return historyFragment;

            default:
                getSupportActionBar().setTitle("Events");
                EventsFragment defaultFragment = EventsFragment.getInstance(position);
                return defaultFragment;

        }

    }

    @Override
    public CharSequence getPageTitle(int position) {

        Drawable drawable = getResources().getDrawable(tabIcons[position]);

        drawable.setBounds(0,0,64,64);

        ImageSpan imageSpan = new ImageSpan(drawable);

        SpannableString spannableString = new SpannableString(" ");

        spannableString.setSpan(imageSpan, 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        return spannableString;
    }

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

我也尝试在 onCreate 方法中通过以下方式设置应用条的标题:

 getSupportActionBar().setTitle("Test");

这个方法也不起作用。

我该怎么解决呢?谢谢!


注意:每当我尝试对操作栏进行任何操作,例如使用以下代码设置返回箭头时:getSupportActionBar.setDisplayHomeAsUpEnabled(true); 似乎什么也没有发生。 - Kenny Gunderman
2个回答

3

MainActivity对象上调用setTitle方法。

MainActivity内部调用:setTitle(...)。从不同的类(MyPagerAdapter)中调用:activity.setTitle(...)

activity是对MainActivity对象的引用。您可以使用getActivity()返回与片段关联的活动。

因此,使用getActivity().setTitle(...);来从片段中设置标题。


1
这是我一个项目的代码:

here is code from one of my projects:

ActionBar actionBar = ((AboutActivity)context).getSupportActionBar();
actionBar.setCustomView(R.layout.action_bar_title_layout);
LinearLayout layout = (LinearLayout) actionBar.getCustomView();
TextView titleTextView = (TextView) layout.findViewById(R.id.page_title_layout);
titleTextView.setText("Your title");
titleTextView.setTypeface(Constants.TYPE_FACE_FONT_MEDIUM); // here you can customise your font
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);

action_bar_title_layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/page_title_layout"
        android:layout_width="wrap_content"
        android:singleLine="true"
        android:textSize="20dp"
        android:textColor="@color/primary_ultralight"
        android:text="Title"
        android:alpha="0.9"
        android:layout_height="wrap_content" />
</LinearLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接