安卓系统。在自定义标题栏中添加按钮[英] Android: Adding button to custom title bar

本文是小编为大家收集整理的关于安卓系统。在自定义标题栏中添加按钮的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我已创建了一个自定义标题栏,如此示例中所示

http://stradizatetype.wordpress .com/2011/03/18/Android-Dynamic-and-Custom-Title-Bars/

"自定义标题栏" - 半路.

在一些活动上,我想放在标题栏的右侧的按钮(与Facebook应用程序相同).我试图将一个按钮添加到视图中,但它不会出现.

自定义标题栏显示如下

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

尝试按如下方式添加按钮.该按钮最终将是一个图像禁止并与自定义标题栏的右侧 - 如果我搞定工作. (刚才实现了我现在太多的layoutparams,但这不是影响按钮显示的))

    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();

我可以使用已经嵌入的按钮创建另一个自定义标题栏,但动态解决方案会更好.

欢呼

推荐答案

首先,感谢您对我博客的链接.其次,让我看看我是否无法回答你.当要添加按钮时,您无法添加另一个按钮的原因之一就是在该示例中,我留下了无法通过通常的频道检索标题栏视图的方法.因此,让我们修复它(并且可能让我在未来周末写另一个博客.)

从XML文件开始,添加id属性:

<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>

和此代码告诉您如何在您的活动中获取该按钮(您必须稍后添加所有flair):

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

如果你看看,标题栏中有一个非描述性按钮(就像我说的那样,你必须添加自己的Flair):

标题栏与另一个按钮

但是,由于我无法保证按钮仍然存在或不会留在后续活动中.我还没有调查它,但这应该让你开始.如果您有任何问题,请随时向他们询问(更多的眼球)或我的博客.

其他推荐答案

这是一种方法来确保按钮保留在标题栏中:

如何创建自定义窗口标题Android

基本上,包装了Android活动类,然后从该新类扩展.

本文地址:https://www.itbaoku.cn/post/102427.html

问题描述

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

http://staticallytyped.wordpress.com/2011/03/18/android-dynamic-and-custom-title-bars/

"A custom title bar" - half way down.

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);

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.

Cheers

推荐答案

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.)

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>

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):

Title Bar with another button

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.

其他推荐答案

Here's one approach to make sure the button(s) remain in the title bar:

How to Create Custom Window Title in Android

Essentially, wrap the android Activity class, and then extend from that new class.