如何Android Holo主题风格对话框按钮

时间:2012-03-21 11:58:16

标签: android android-theme

我正在Holo Theme上创建一个对话框,并希望按照操作系统默认的方式显示按钮。到目前为止,我已经创建了对话框,但按钮不会像在Holo for ICS中完成的应用程序那样呈现。 我怎样才能做到这一点? 我想要的外观&感觉是 No. 3rd in this image 我能够到达这里 Notice the Signup and Login buttons

3 个答案:

答案 0 :(得分: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 :(得分: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" and的{​​$ 1}}大小合并100:$:layout_weight =“1”按钮

答案 2 :(得分:2)

您可以通过Android Manifest xml或使用setTheme(android.R.style.Theme_Holo);

在Activity的onCreate中设置主题

按钮大小与主题本身无关。大小取决于您的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>