【问题标题】:How to Android Holo Theme style dialog box buttons如何为 Android Holo Theme 样式的对话框按钮
【发布时间】:2012-04-05 22:42:11
【问题描述】:

我正在创建一个 Holo 主题对话框,并希望遵循操作系统默认的按钮显示方式。到目前为止,我已经创建了对话框,但按钮的呈现方式不像在 Holo for ICS 中完成的应用程序那样呈现。 我怎样才能做到这一点? 我的预期外观和感觉是 我能到达这里

【问题讨论】:

    标签: android android-theme


    【解决方案1】:

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

    这对我来说效果很好。

    ...
    <!--
    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"
    

    【讨论】:

    • 小修正:可能您应该在示例的第一个视图中使用 dividerHorizo​​ntal。
    • 小心,dividerHorizo​​ntal 在 API 11 之前的版本中不受支持。
    • @dimsuz 为什么是dividerHorizontal?
    • @KevinOrr 查看顶部问题中的屏幕截图:这是确定|取消按钮上方的分隔线,它是水平分隔线。 IIRC这个答案在第一次发布时有dividerVertical,但作者从那时起编辑了它,现在一切都很好:)
    • @dimsuz 我在 Eclipse 中试用它,但取出它似乎并没有改变任何东西。它在做什么?
    【解决方案2】:

    这是有效的:

    <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% 的权重分布是由于 android:layout_width="match_parent" andandroid:layout_weight="1"`为按钮组合了 LinearLayout 的 100 美元大小

    【讨论】:

    • 感谢您的回答!只是想知道如何或是否能够在按钮周围获得浅灰色边框,如您的第一个屏幕截图所示?谢谢
    • 我没有单独使用这个标记。我很着急.. 非常急于完成这个项目,我不在乎为什么我没有得到它并且我没有纠正它。在一周的时间内,他们要求将其从 ICS 转移到 GB,而我不得不手动创建所有这些,因此浪费了很多精力并且早就被遗忘了 :)
    • 但是,如果我必须再做一次,我会去 Android Dev 网站下载其中一个示例并检查一下
    • 您不应该直接引用样式,请使用 style="?android:attr/borderlessButtonStyle" 文档中使用的 developer.android.com/guide/topics/ui/controls/button.html
    • 这可能是 grate,但这需要 API 级别 14 才能实现这种风格。如果向后兼容性很重要,您有解决方案吗?
    【解决方案3】:

    您可以通过 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 所以我在这里写的 XML 固定了按钮的宽度,但是按钮仍然没有得到 Holo 主题的平面布局?
    • 可悲的是连宽度都没有 :) 对不起.. 我发现了扁平按钮样式,并会​​在解决宽度问题后立即发布答案。奇怪的是,你的代码应该可以工作,但它不工作,我想不出任何其他的宽度
    • 我认为只要将fill_parent 更改为match_parent,宽度就可以正常工作
    • @kishu27 酷。至少我的回答给了你正确的方向:) 很高兴你解决了它。
    猜你喜欢
    • 1970-01-01
    • 2012-04-07
    • 2011-01-28
    • 2011-05-06
    • 1970-01-01
    • 1970-01-01
    • 2011-02-01
    • 1970-01-01
    • 2012-05-04
    相关资源
    最近更新 更多