安卓:添加按钮,自定义标题栏自定义、标题栏、按钮

2023-09-06 14:10:55 作者:情是堕落的伤

我创建了一个自定义标题栏如本例所示

I have created a custom title bar as shown in this example

的http://staticallytyped.word$p$pss.com/2011/03/18/android-dynamic-and-custom-title-bars/

自定义标题栏 - 降了一半

"A custom title bar" - half way down.

在一些活动,我想放在标题栏(同Facebook的应用程序)的右侧的按钮。我已尝试添加一个按钮的视图如下,但它不会出现。

On some activities I would like to place a button on the right hand side of the titlebar (same as facebook app). I have attempted to add a button to the view as follows, but it doesn't appear.

显示自定义标题栏如下:

Custom title bar is displayed as follows

 requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);  的setContentView(R.layout.maintabhost);  。getWindow()setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.headerbar_include);

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.maintabhost); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.headerbar_include);

试图添加按钮,如下所示。该按钮将最终成为一个ImageButton的并对齐,以自定义的右侧标题栏,如果我得到它的工作。 (刚刚意识到我有太多的LayoutParams了,但这个心不是影响按钮显示)

Attempting to add button as follows. The button will eventually be an ImageButton and aligned to right of custom titlebar-if I get it working. (just realised I've too many layoutparams now, but this isnt affecting the button display)

    LinearLayout layout = (LinearLayout) findViewById(R.id.headerbar);
    Button searchButton = new Button(this);
    searchButton.setText("info");

    LayoutParams layoutParams = new LinearLayout.LayoutParams
    (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    searchButton.setLayoutParams(new LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));

    layout.addView(searchButton, layoutParams);
    layout.invalidate();

我可以创建另一个自定义标题栏与已经嵌入的按钮,而是一个动态的解决方案会更好。

I could just create another custom titlebar with the button already embedded, but a dynamic solution would be better.

干杯

推荐答案

首先,感谢您的链接到我的博客。其次,让我看看,如果我不能回答你。其中一项您无法添加另一个按钮,当你想添加一个按钮的原因之一是,在这个例子中,我离开你,没有办法通过正常渠道获取的标题栏查看。因此,让我们来修复它(并有可能让我写另一篇博客本周末。)

First of all, thanks for the link to my blog. Second, let me see if I can't answer that for you. One of the reasons you're having trouble adding another button when you want to add a button is that in that example I left you with no way of retrieving the Title Bar View through the usual channels. Hence, let's fix it (and potentially let me write another blog post this coming weekend.)

与XML文件开始,添加一个id属性:

Starting with the xml file, add an id attribute:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="horizontal"
    android:id="@+id/title_complex">
    <!-- Stuff -->
</LinearLayout>

和这里的code向您展示如何获取按钮,在那里,你的活动中(你必须添加所有的天赋后):

and here's code to show you how to get that button in there within your Activity (you'll have to add all the flair later):

LinearLayout layout = (LinearLayout) getWindow().findViewById(R.id.title_complex);
layout.addView(new Button(this));

如果你看看,有一个在标题栏中显示的非描述按钮(就像我说的,你必须添加自己的天赋):

and if you take a look, there's a non-descript button in the Title Bar (like I said, you'll have to add your own flair):

由于笔记,但是,我不能保证该按钮将保持或将不会留在后续活动。我没有调查这些事,但这应该让你开始。如果你有任何问题,请随时问他们在这里(更多的眼球),或在我的博客。

Due note, however, that I can't guarantee that the button will remain or won't remain on subsequent Activities. I haven't investigated it yet but this should get you started. And if you have any more questions, feel free to ask them here (more eyeballs) or on my blog.