更改工具栏Textview标题

时间:2016-10-27 02:23:50

标签: android android-studio android-toolbar

我有切换 Actionbar to Toolbar

为了更好的设计,我更改了工具栏的字体 使用Textview和此代码

Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
TextView toolbarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/NewFont.ttf");
toolbarTitle.setTypeface(myTypeface);

现在所有设置片段页面上的相同文本设置

我有测试这个

private void setTitleText(String Title) {

    Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
    TextView toolbarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
    Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/NewFont.ttf");
    toolbarTitle.setText(Title);
    toolbarTitle.setTypeface(myTypeface);
}

这里是片段之一

public static class GeneralPreferenceFragment extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref_general);
    //with this i call the title change
        setTitleText(R.string.pref_header_general);

        setHasOptionsMenu(true);

        // Bind the summaries of EditText/List/Dialog/Ringtone preferences
        // to their values. When their values change, their summaries are
        // updated to reflect the new value, per the Android Design
        // guidelines.
        bindPreferenceSummaryToValue(findPreference("example_text"));
        bindPreferenceSummaryToValue(findPreference("example_list"));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            startActivity(new Intent(getActivity(), SettingsActivity.class));
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

这不起作用我有这个错误信息

Non-static method 'setTitleText(java.lang.String)' cannot be referenced from a static context

修改

我找到了一个简单的方法

我已经改变了这个

Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
TextView toolbarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/NewFont.ttf");
toolbarTitle.setTypeface(myTypeface);

    TextView toolbarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
    Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/NewFont.ttf");
    toolbarTitle.setText(toolbar.getTitle());
    toolbarTitle.setTypeface(myTypeface);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

现在一切正常

1 个答案:

答案 0 :(得分:-1)

您无法在static方法/类中调用非static方法。您需要删除' static'来自您的Fragment

public static class GeneralPreferenceFragment extends PreferenceFragment