简体   繁体   English

无法将自定义按钮添加到Android标题栏

[英]Failing to add custom Buttons to Android Title Bar

I have a main Activity and I want to add a couple of buttons to the ActionBar . 我有一个主要的Activity ,我想向ActionBar添加几个按钮。 As I understand it, it's the bar with the title of the app and the 3 dots that function as a menu. 据我了解,它是带有应用程序标题和3个点的菜单栏。 This is the code I've implementend: 这是我实现的代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    .....
    ActionBar ab = getActionBar();
    ab.setCustomView(new TitleButtonView(this));
 }

And TitleButtonView is simply: TitleButtonView很简单:

public class TitleButtonView extends LinearLayout {

    public TitleButtonView(Context context) {
        super(context);
        this.setLayoutParams(new LinearLayout.LayoutParams(200,50));
        Button b = new Button(context);
        Button b1 = new Button(context);
        b.setLayoutParams(new LinearLayout.LayoutParams(100,50));
        b1.setLayoutParams(new LinearLayout.LayoutParams(100,50));
        setOrientation(LinearLayout.HORIZONTAL);
        addView(b);
        addView(b1);
        b.setText("Boton");
        b1.setText("Otro");
    }
}

What am I doing wrong? 我究竟做错了什么? Is it not working because the setCustoView is used for something else? 是否因为setCustoView用于其他用途而无法工作?

Well after reading a little bit more, I found that this is the line that needs to be added. 在阅读了更多内容之后,我发现这是需要添加的行。

ab.setDisplayOptions(ab.getDisplayOptions()|ActionBar.DISPLAY_SHOW_CUSTOM); ab.setDisplayOptions(ab.getDisplayOptions()| ActionBar.DISPLAY_SHOW_CUSTOM);

It tells the action bar to show custom views. 它告诉操作栏显示自定义视图。

Sorry for any trouble 对不起有什么麻烦

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM