【问题标题】:Android : Decreasing size of the ButtonAndroid:减小按钮的大小
【发布时间】:2013-10-24 04:14:00
【问题描述】:

我在其中一种布局中使用Button。我希望Button 的大小与文本/标签的大小一样小。

我正在使用以下代码。

<Button 
android:text="@string/cancle_button" 
android:id="@+id/button1" 
android:background="@drawable/back_button" 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="12sp"
android:textColor="#ffffff"
android:textStyle="bold" />

但我希望Button 高度与文本/标签高度相同。每当我减少或增加文本高度时,Button 高度保持不变。我该如何解决这个问题?

【问题讨论】:

  • 当您在后台传递图像时,按钮大小将取决于图像大小,然后取决于您的文本大小。因此,如果您的文本大小小于图像,它不会减小。但是,如果文本大小大于图像,那么它会增长以适合文本。这一切都是因为“wrap_content”属性而发生的。
  • 感谢 Arshad,但在这里我在我的可绘制文件夹中使用 .xml 形式的渐变绘制。
  • 即使这样,渐变也有固定的大小和形状。所以作为图像本身起作用。
  • 最好的办法是根据文本大小调整渐变大小以完成工作。

标签: android android-button


【解决方案1】:

问这个问题已经快一年了。但我现在面临这个问题并得到了解决方案。 :) 也许它会帮助某人。

您可以通过为 Button 小部件设置 android:minHeight="0dp" 来实现此目的。

我猜这种行为是因为考虑到最小触摸区域,框架为 Button 小部件设置了一些默认值(可能是 48dp)。

【讨论】:

    【解决方案2】:

    使用可以在这种情况下拍摄图像视图,或者如果您在这种情况下拍摄文本视图,以便您可以拍摄具有适当尺寸的图像,这样它就不会通过这些措施变得更蓝您可以为图像设置小尺寸最好的方法是您可以拍摄您想要创建它的好方法的大小的图像

    【讨论】:

    • 在这个要求中,我在我的drawable文件夹中使用渐变形状drawable
    【解决方案3】:

    您可以通过将按钮高度设置为与标签高度相同的特定值来解决此问题。

    <Button 
    android:text="CANCEL" 
    android:id="@+id/button1" 
    android:background="@drawable/back_button" 
    android:layout_width="wrap_content"
    android:layout_height="20sp"
    android:gravity="center"
    android:textSize="15sp"
    android:textColor="#ffffff"
    android:textStyle="bold" />
    

    【讨论】:

    • 感谢您的回复,有没有其他方法可以让按钮自动调整大小而无需硬编码。我希望按钮大小在文本大小发生变化时也会发生变化
    • 如果你改变文本的大小,那么当高度设置为换行内容时,它会自动调整该高度
    • 不调整按钮高度保持不变。
    • 您只需将按钮换行内容的高度并更改它会自动调整的文本大小。
    【解决方案4】:

    您可以使用相对布局来实现这一点。检查此示例代码,

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="TextView" />
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/textView1"
            android:layout_alignTop="@id/textView1"
            android:layout_alignBottom="@id/textView1"
            android:text="Button" />
    
    </RelativeLayout>
    

    【讨论】:

      【解决方案5】:

      您可以通过更改按钮的 layout_weight 来解决此问题,因为默认情况下它设置为“1”

      只需将其设置为 0,然后只需更改按钮的 layout_height 即可更改 button_height。 这是普通按钮的代码-

          <Button
              android:id="@+id/button1"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_weight="0"
              android:text="Button" />
      

      【讨论】:

        【解决方案6】:
                <com.google.android.material.button.MaterialButton
                    android:layout_width="36dp"
                    android:layout_height="36dp"
                    android:layout_marginTop="8dp"
                    android:layout_marginBottom="8dp"
                    android:background="@drawable/custom_small_button_bg"
                    android:drawableLeft="@drawable/ic_minus"
                    android:paddingStart="6dp"
                    android:paddingLeft="6dp" />
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-09-12
          • 2016-11-16
          • 2018-12-30
          • 1970-01-01
          • 1970-01-01
          • 2023-03-28
          • 1970-01-01
          相关资源
          最近更新 更多