在XML中,Button的大小随其Font大小而变化 [英] In XML Button size changes with its Font size

查看:203
本文介绍了在XML中,Button的大小随其Font大小而变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这听起来很简单,但是我想更改按钮的字体大小以填充Button.即使当我减小文本高度(例如,Button的高度也减小)时,文本并没有占据按钮内部的所有空间.有什么方法可以更改文本大小,以便它可以填充Button内的空白,还是只需要使用Image Button即可.

I know this sounds simple but I wanted to change a button's font size to fill the Button .Even though the text doesn't take all the space inside the button when I decrease text height for example the Button's height decreases as well.Is there any way I can change The text-size so it fills that space inside the Button or do I have to just use an Image Button .

在这种情况下:-

        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="@android:color/black"
        android:id="@+id/led"
        >


         <Button
            android:id="@+id/grow"
            android:layout_width="match_parent"
            android:layout_height="39dp"
            android:layout_marginRight="1dp"
            android:layout_weight="1"
            android:textSize="15dp"
            android:background="@color/Lightbrown"
            android:text="A▲"

            android:textAllCaps="false"
         />

        <Button
            android:layout_width="match_parent"
            android:layout_height="39dp"
            android:text="A▼"
            android:textAllCaps="false"
            android:id="@+id/shrink"
            android:background="@color/Lightbrown"
            android:layout_marginRight="1dp"
            android:layout_weight="1"
            android:padding="0dp"
            android:textSize="10dp"

            />

请参阅我将我的Linearlayout用作Button的背景,第二个按钮的大小随其字体大小而改变,我只希望其大小与第一个Button保持相同,但具有较小的textsize.

See I used my Linearlayout as a background for my Buttons the second button's size changes with its font size I just want its size to remain the same as the first Button but with a smaller textsize.

推荐答案

更新

您的第二个按钮实际上并不小,只是以您不一定期望的方式对齐.

Update

Your second button is not actually smaller, it is just aligned in a way you wouldn't necessarily expect.

水平LinearLayout将会使这些子基线对齐".这意味着他们将确保该行中所有文本的底边处于相同的高度.由于第二个按钮使用的是较小的文本,所以文本底部放在按钮内较高,因此LinearLayout迫使整个按钮向下适应.

Horizontal LinearLayouts with TextView (or subclass, which Button is) children will "baseline align" the children. That means they will make sure that the bottom edge of all the text in the row is at the same height. Since your second button uses smaller text, the text bottom would be higher up inside the button, so the LinearLayout forces the whole button down to accomodate.

将此属性添加到您的LinearLayout:

android:baselineAligned="false"

原始

首先,我假设您正在使用android:layout_height="wrap_content".如果您不希望按钮的高度与字体大小成比例,则必须将其更改为某个固定值(如果您希望它的大小与父按钮的大小相同,则为match_parent.)

Original

First, I assume you're using android:layout_height="wrap_content". If you don't want your button's height to scale with font size, you'll have to change this to some fixed value (or match_parent if you want it to be the same size as its parent).

至于为什么文本不占所有空间",这是因为Button具有自动内置的填充.您可以通过定义android:padding="0dp"

As for why the text "doesn't take up all the space", that's because Buttons have padding built into them automatically. You can remove this padding by defining android:padding="0dp"

但是,您很快就会注意到,如果不为其提供填充和太大的文本,则该按钮看起来真的很糟糕.如何解决这个问题实际上取决于您的设计要求.

However, you'll soon notice that the button looks really bad if you give it no padding and too-large text. How to solve that is really up to the requirements of your design.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="32dp"
        android:layout_gravity="center_horizontal"
        android:padding="0dp"
        android:textSize="36sp"
        android:text="Hello world"/>

</FrameLayout>

这篇关于在XML中,Button的大小随其Font大小而变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆