Android:设置自定义标题/操作栏 - 不工作

时间:2022-12-04 23:44:11

I am trying to set a custom Title Bar as follows (please note this activity extends from FragmentActivity if it matters):

我正在尝试如下设置自定义标题栏(请注意,如果重要,此活动将从FragmentActivity扩展):

...
import android.support.v4.app.FragmentActivity;
...
public class MyActivity extends FragmentActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        android.app.ActionBar actionBar = this.getActionBar();
        LayoutInflater inflator = (LayoutInflater) this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflator.inflate(R.layout.window_title, null);    
        TextView tv = (TextView) v.findViewById(R.id.titlex);    
        tv.setText("My Title");

        actionBar.setCustomView(v);                

        setContentView(R.layout.myactivity);
    }
    ...
}

But i do not see the Custom Title Bar at all, what is missing?

但我根本没有看到自定义标题栏,缺少什么?

1 个解决方案

#1


Set actionbar.setDisplayShowCustomEnabled(true); before actionBar.setCustomView(v);

设置actionbar.setDisplayShowCustomEnabled(true);在actionBar.setCustomView(v)之前;

EDIT

Here i past my code for Custom ActionBar creation

在这里,我通过自定义ActionBar创建的代码

// Action Bar Customization
    ActionBar ab =act.getActionBar();

    ColorDrawable colorDrawable = new ColorDrawable(act.getResources().getColor(color.ActionBar_bg));
    ab.setBackgroundDrawable(colorDrawable);


    ab.setDisplayShowTitleEnabled(false); // disables default title on
                                            // actionbar.
    ab.setDisplayShowCustomEnabled(true); // enables custom view.
    ab.setDisplayShowHomeEnabled(false); // hides app icon.
    ab.setTitle("");
    // Inflating Layout
    LayoutInflater inflater = (LayoutInflater) act.getActionBar()
            .getThemedContext().getSystemService(LAYOUT_INFLATER_SERVICE);

    View customActionBar = inflater.inflate(R.layout.actionbar_layout, null);
    txtUserName=(TextView)customActionBar.findViewById(R.id.txtUserName);

    ab.setCustomView(customActionBar);

#1


Set actionbar.setDisplayShowCustomEnabled(true); before actionBar.setCustomView(v);

设置actionbar.setDisplayShowCustomEnabled(true);在actionBar.setCustomView(v)之前;

EDIT

Here i past my code for Custom ActionBar creation

在这里,我通过自定义ActionBar创建的代码

// Action Bar Customization
    ActionBar ab =act.getActionBar();

    ColorDrawable colorDrawable = new ColorDrawable(act.getResources().getColor(color.ActionBar_bg));
    ab.setBackgroundDrawable(colorDrawable);


    ab.setDisplayShowTitleEnabled(false); // disables default title on
                                            // actionbar.
    ab.setDisplayShowCustomEnabled(true); // enables custom view.
    ab.setDisplayShowHomeEnabled(false); // hides app icon.
    ab.setTitle("");
    // Inflating Layout
    LayoutInflater inflater = (LayoutInflater) act.getActionBar()
            .getThemedContext().getSystemService(LAYOUT_INFLATER_SERVICE);

    View customActionBar = inflater.inflate(R.layout.actionbar_layout, null);
    txtUserName=(TextView)customActionBar.findViewById(R.id.txtUserName);

    ab.setCustomView(customActionBar);