如何将Android Holo主题样式应用于对话框按钮

52
我正在创建一个Holo主题的对话框,并希望遵循操作系统默认的显示按钮方式。到目前为止,我已经创建了对话框,但是按钮没有以ICS中使用的应用程序中的方式呈现。 我该怎么做呢? 我打算的外观和感觉是 图像中的第三个 而我能够达到这里 注意注册和登录按钮
3个回答

85

有点晚了,但也许还有人对此感兴趣。

这对我来说运行得非常好。

...
<!--
EDIT: be carefull, "?android:attr/dividerHorizontal" is only supported since API 11
      just avoid it in prior OSs.
-->
<View
    android:layout_width="fill_parent"
    android:layout_height="1dip"
    android:background="?android:attr/dividerHorizontal" />
<LinearLayout 
    style="?android:attr/buttonBarStyle"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingTop="0dip"
    android:paddingLeft="2dip"
    android:paddingRight="2dip"
    android:measureWithLargestChild="true">

    <Button 
        android:id="@+id/cancel"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@android:string/cancel"/>
    <Button 
        android:id="@+id/ok"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@android:string/ok"/>
</LinearLayout>
...

加载此布局的活动需要 Holo.Dialog 主题。

android:theme="@android:style/Theme.Holo.Dialog"

1
小修正:在你的示例中,可能应该在第一个视图中使用dividerHorizontal。 - dimsuz
1
请注意,dividerHorizontal在API 11之前不受支持。 - Mario Lenci
@dimsuz 为什么要使用 dividerHorizontal - KevinOrr
@KevinOrr 看一下问题顶部的截图:这是位于“确定”和“取消”按钮上方的分隔线,它是水平的。如果我没记错的话,这个答案最初发布时具有垂直分隔线,但作者此后对其进行了编辑,现在一切都好了 :) - dimsuz
@dimsuz 我在Eclipse中尝试了一下,但是删除它似乎没有改变任何东西。它在做什么? - KevinOrr

22

这是可行的方法:

<LinearLayout
    android:id="@+id/buttonHolder"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:orientation="horizontal"
    >

    <Button
        android:id="@+id/cmdSignup"
        style="@android:style/Widget.Holo.Light.Button.Borderless.Small"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/Signup" />

    <Button
        android:id="@+id/cmdLogin"
        style="@android:style/Widget.Holo.Light.Button.Borderless.Small"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/Login" />
</LinearLayout>

使用style="@android:style/Widget.Holo.Light.Button.Borderless.Small"属性可以使按钮看起来扁平化,而50%的宽度分配是通过将LinearLayout的尺寸设置为android:layout_width="match_parent"并将按钮的权重设置为android:layout_weight="1"实现的。


谢谢您的回答!我想知道您是如何或者是否能够像第一张截图中那样给按钮加上浅灰色边框的?谢谢。 - Dittimon
仅凭这个标记,我无法理解它。当时我很匆忙,非常匆忙地完成项目,我没有关心为什么我无法理解它,也没有进行更正。一周后,他们要求将其从ICS转移到GB,因此我不得不手动创建所有内容,所以努力白费并被长时间遗忘 :) - kishu27
但是,如果我再次做这件事,我会去下载 Android Dev 网站上的一个示例并检查它。 - kishu27
5
不应直接引用样式,而应使用 style="?android:attr/borderlessButtonStyle",该样式在文档 http://developer.android.com/guide/topics/ui/controls/button.html 中有说明。 - smith324
2
这很棒,但是需要 API Level 14 才能使用此样式。如果需要向后兼容,你有解决方案吗? - Adorjan Princz

2

您可以通过Android Manifest xml或在Activity的onCreate中使用setTheme(android.R.style.Theme_Holo);来设置主题。

按钮的大小与主题本身无关。大小取决于您的xml定义。 从您发送的图片中看,按钮似乎已经接收到了Holo主题,所以这里没有问题...

这是一个xml布局,它将拉伸按钮以填充整个对话框宽度:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
    <LinearLayout
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dip"
                >
                <Button
                    android:id="@+id/okButton"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="OK"
                />
                <Button
                    android:id="@+id/cancelButton"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Cancel"
                />          
        </LinearLayout>
</LinearLayout>

抱歉,但这个不起作用。我同意关于宽度的问题,但我无法让对话框上的按钮看起来平坦。 - kishu27
@kishu27,所以我在这里编写的XML固定了按钮的宽度,但按钮仍然没有获得Holo主题的平面布局? - Lior Iluz
遗憾的是,甚至宽度都不行 :) 我很抱歉...我发现了扁平按钮样式,并在解决宽度问题后尽快发布答案。奇怪的是,你的代码本应该能够运行,但它却无法工作,我也想不到其他什么办法来解决宽度问题。 - kishu27
我认为一旦我将fill_parent更改为match_parent,宽度就可以正常工作了。 - kishu27
1
@kishu27 很酷。至少我的答案给了你正确的方向 :) 很高兴你解决了它。 - Lior Iluz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接