Change Android Dialog button text size via styles

旧时模样 提交于 2019-12-23 07:12:59

问题


I am trying to enlarge the text size on all of my applications dialog buttons via styles. The following code will change the buttons background colour and even the text case but for some reason the textSize item is not honoured:

<style name="MyAppTheme" parent="@android:style/Theme.Holo">
    <item name="android:dialogTheme">@style/MyApp.Dialog</item>
    <item name="android:alertDialogTheme">@style/MyApp.Dialog.Alert</item>
</style>

<style name="MyApp.Dialog" parent="@android:style/Theme.Holo.Dialog">
    <item name="android:borderlessButtonStyle">@style/MyApp.BorderlessButton</item>
</style>

<style name="MyApp.Dialog.Alert" parent="@style/MyApp.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
</style>

<style name="MyApp.BorderlessButton" parent="@android:style/Widget.Holo.Button.Borderless">
    <item name="android:textSize">50sp</item>
    <item name="android:textAllCaps">true</item>
    <item name="android:background">#800</item>
</style>

Why is the textSize not being read? What do I have to do to make it larger?


回答1:


You are not assigning the right attribute here:

<style name="MyApp.Dialog" parent="@android:style/Theme.Holo.Dialog">
====>    <item name="android:borderlessButtonStyle">@style/MyApp.BorderlessButton</item>
</style>

The attribute you need to use is:

android:buttonStyle

I propose the following change:

<style name="MyApp.Dialog" parent="@android:style/Theme.Holo.Dialog">
    <item name="android:buttonStyle">@style/MyApp.BorderlessButton</item>
</style>

Why:

borderlessButtonStyle is a conveniently defined style. But it isn't currently wired to any of the default attributes - in this case, buttonStyle. You still need to assign it.

I can't figure out why the background and textAllCaps attributes come into effect. Posting your dialog's/alert dialog's xml might help. Also, how have you defined the string - "i agree" or "I AGREE"?




回答2:


A buttons in a AlertDialog use the android:buttonBarButtonStyle in Holo. Override that attribute in your themes/styles and set the android:minHeight="0dp" to wrap the content, or of course your can provider your own height.

Correction: I mis-read the title. You of course can set the android:textSize="32sp" on that same style to change the text size.

Here is the layout for it from the framework:

<LinearLayout android:id="@+id/buttonPanel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="@dimen/alert_dialog_button_bar_height"
    android:orientation="vertical"
    android:divider="?android:attr/dividerHorizontal"
    android:showDividers="beginning"
    android:dividerPadding="0dip">
    <LinearLayout
        style="?android:attr/buttonBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layoutDirection="locale"
        android:measureWithLargestChild="true">
        <Button android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_gravity="start"
            android:layout_weight="1"
            android:maxLines="2"
            style="?android:attr/buttonBarButtonStyle"
            android:textSize="14sp"
            android:minHeight="@dimen/alert_dialog_button_bar_height"
            android:layout_height="wrap_content" />
        <Button android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1"
            android:maxLines="2"
            style="?android:attr/buttonBarButtonStyle"
            android:textSize="14sp"
            android:minHeight="@dimen/alert_dialog_button_bar_height"
            android:layout_height="wrap_content" />
        <Button android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_gravity="end"
            android:layout_weight="1"
            android:maxLines="2"
            android:minHeight="@dimen/alert_dialog_button_bar_height"
            style="?android:attr/buttonBarButtonStyle"
            android:textSize="14sp"
            android:layout_height="wrap_content" />
    </LinearLayout>
 </LinearLayout>



回答3:


I know that it is not answer to your problem but why you don't want to style Activity to look like Dialog? I had the same problem like you and styling Android Dialog is really painful. Using Activity you can put whatever you want as xml content and style it as you want. Also you have ActionBar so you can add menu to your Dialog and use custom views in ActionBar. Maybe it is time to change your Android Dialogs to Activity with Dialog style.




回答4:


Try this style:

<style name="MyApp.BorderlessButton" parent="@android:style/Widget.Holo.Button.Borderless">
   <item name="android:textSize">50sp</item>
   <item name="android:textAllCaps">true</item>
   <item name="android:background">#800</item>
</style>


来源:https://stackoverflow.com/questions/21569301/change-android-dialog-button-text-size-via-styles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!