简体   繁体   English

在XML中,按钮大小随其字体大小而变化

[英]In XML Button size changes with its Font size

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 . 我知道这听起来很简单,但是我想更改按钮的字体大小以填充按钮。即使当我减小文本高度时文本并没有占据按钮内部的所有空间,例如按钮的高度也减小了。我可以更改文本大小的方式,以使其填充Button内部的空间,或者我只需要使用Image Button即可。

Here is the case :- 情况是这样的:

在此处输入图片说明

        <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"

            />

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. 请参见我将Linearlayout用作Button的背景,第二个按钮的大小随其字体大小而改变,我只希望其大小与第一个Button保持相同,但使用较小的textsize。

Update 更新

Your second button is not actually smaller, it is just aligned in a way you wouldn't necessarily expect. 您的第二个按钮实际上并不小,只是以您不一定期望的方式对齐。

Horizontal LinearLayout s with TextView (or subclass, which Button is) children will "baseline align" the children. 具有TextView (或子类, Button是)子级的Horizo​​ntal LinearLayout可以使这些子级“基线对齐”。 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强制整个按钮向下适应。

Add this attribute to your LinearLayout : 将此属性添加到您的LinearLayout

android:baselineAligned="false"

Original 原版的

First, I assume you're using android:layout_height="wrap_content" . 首先,假设您使用的是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). 如果您不希望按钮的高度与字体大小成比例,则必须将其更改为某个固定值(如果希望与父match_parent大小相同,则将其更改为match_parent )。

As for why the text "doesn't take up all the space", that's because Button s have padding built into them automatically. 至于为什么文本“不会占用所有空间”,这是因为Button会自动在其中内置填充。 You can remove this padding by defining android:padding="0dp" 您可以通过定义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>

在此处输入图片说明

Use a fixed size instead of wrap_content for your button. 为按钮使用固定大小而不是wrap_content

f that doesn't work for your design, consider overlaying a TextView on top of your Button (with ConstraintLayout or FrameLayout or RelativeLayout). 如果对您的设计不起作用,请考虑将TextView覆盖在Button顶部(使用ConstraintLayout或FrameLayout或RelativeLayout)。 After that, you can set the TextView's focusable, focusableInTouchMode, and clickable properties to false so that it doesn't intercept your Button's clicks. 之后,您可以将TextView的focusable,focusableInTouchMode和clickable属性设置为false,以使其不会拦截Button的单击。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM