【发布时间】:2012-04-05 22:42:11
【问题描述】:
我正在创建一个 Holo 主题对话框,并希望遵循操作系统默认的按钮显示方式。到目前为止,我已经创建了对话框,但按钮的呈现方式不像在 Holo for ICS 中完成的应用程序那样呈现。 我怎样才能做到这一点? 我的预期外观和感觉是 我能到达这里
【问题讨论】:
标签: android android-theme
我正在创建一个 Holo 主题对话框,并希望遵循操作系统默认的按钮显示方式。到目前为止,我已经创建了对话框,但按钮的呈现方式不像在 Holo for ICS 中完成的应用程序那样呈现。 我怎样才能做到这一点? 我的预期外观和感觉是 我能到达这里
【问题讨论】:
标签: android android-theme
有点晚了,但也许有人对此仍然感兴趣。
这对我来说效果很好。
...
<!--
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"
【讨论】:
dividerHorizontal?
这是有效的:
<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 美元大小
【讨论】:
style="?android:attr/borderlessButtonStyle" 文档中使用的 developer.android.com/guide/topics/ui/controls/button.html
您可以通过 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>
【讨论】:
fill_parent 更改为match_parent,宽度就可以正常工作