【问题标题】:How to create a square button (90 degree angles) Android Studio如何创建一个方形按钮(90度角)Android Studio
【发布时间】:2021-06-11 14:23:50
【问题描述】:

因此,在 Android Studio 中摆弄时,我提出了一个问题:'如何使按钮的角变成正方形?环顾互联网和堆栈溢出,我得到了很多答案,但我想知道是否有更简单的方法......然后噗!在XML设计的Common attributes下看到了背景,好奇我在里面放了'@android:color/white',然后圆角方形按钮变成了90度方形按钮。

这是一个不错的惊喜。由于我没有看到有人谈论这样做,所以我决定在这里发帖。希望它对您的按钮上的 90 度角有所帮助和快速破解!

Back(有背景)和home(无背景)的代码区别

<Button
    android:id="@+id/back"
    android:background="@color/white"
    android:text="Back"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.008" />

<Button
    android:id="@+id/home"
    android:text="Home"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.007" />

【问题讨论】:

    标签: android xml android-studio android-button material-components-android


    【解决方案1】:

    你需要为你的按钮创建背景xml文件

    你可以给任何形状

    【讨论】:

      【解决方案2】:

      要创建方形按钮,您可以创建一个可绘制资源文件,然后制作根元素形状,然后您可以为形状赋予颜色或笔触,然后要使其成为方形,您必须传递相同的高度和相同的宽度,例如宽度 100dp 和高度 100dp。

      下面是一个示例,说明如何获得一个正方形的可绘制对象:

      <?xml version="1.0" encoding="utf-8"?>
      <shape xmlns:android="http://schemas.android.com/apk/res/android">
          <solid android:color="@color/white"/>
          <size android:width="100dp" android:height="100dp"/>
      </shape>
      

      【讨论】:

        【解决方案3】:

        要实现方形按钮,只需使用属性app:cornerRadius="0dp"

            <com.google.android.material.button.MaterialButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="BUTTON"
                app:cornerRadius="0dp"/>
        

        请注意,如果您使用的是 Material Components 主题,Button 在运行时会替换为 MaterialButton

        注意android:background="@color/white"。 使用android:background 属性,MaterialButton 不使用它自己的MaterialShapeDrawable 作为背景。这意味着形状外观、笔划和圆角等特征将被忽略。
        这是因为您正在获得一个方形按钮。

        【讨论】:

          猜你喜欢
          • 2015-09-07
          • 1970-01-01
          • 2017-05-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-01-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多