我正在创建一个Holo主题的对话框,并希望遵循操作系统默认的显示按钮方式。到目前为止,我已经创建了对话框,但是按钮没有以ICS中使用的应用程序中的方式呈现。
我该怎么做呢?
我打算的外观和感觉是
而我能够达到这里
![注意注册和登录按钮](https://istack.dev59.com/FxDaO.webp)
![图像中的第三个](https://istack.dev59.com/PrOwq.webp)
![注意注册和登录按钮](https://istack.dev59.com/FxDaO.webp)
有点晚了,但也许还有人对此感兴趣。
这对我来说运行得非常好。
...
<!--
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"
这是可行的方法:
<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"
实现的。
style="?android:attr/borderlessButtonStyle"
,该样式在文档 http://developer.android.com/guide/topics/ui/controls/button.html 中有说明。 - smith324您可以通过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
,宽度就可以正常工作了。 - kishu27
dividerHorizontal
? - KevinOrr