材料按钮未填充整个屏幕宽度

时间:2021-06-11 03:45:20

标签: java android xml material-design

我对整个应用程序开发内容还很陌生,我只是想学习基础知识。特别是对于 XML,我只有很少的经验。 我的问题是,我想添加一个按钮,它会扩展到整个屏幕宽度,但它不起作用并在它周围留下某种边距。我已经尝试了很多,但我找不到任何解决方案,或者我只是不知道该搜索什么。我发现 android:insets 属性的一个提示不起作用,除了顶部和底部插入。

enter image description here

这是我的组件树:

enter image description here

这是 XML:

<?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bg_2"
        tools:context=".ApplicationActivity">
    
        <com.google.android.material.button.MaterialButton
            android:id="@+id/backBtn"
            android:layout_width="450dp"
            android:layout_height="59dp"
            android:background="@drawable/back_btn_bg"
            android:fontFamily="@font/montserrat_thin"
            android:insetLeft="0dp"
            android:insetTop="0dp"
            android:insetRight="0dp"
            android:insetBottom="0dp"
            android:text="@string/back_btn"
            app:backgroundTint="#470000"
            app:cornerRadius="0dp"
            app:fontFamily="@font/montserrat_thin"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

我希望我包含了所有需要的信息。 提前谢谢你:)

2 个答案:

答案 0 :(得分:1)

只需将 layout_width 更改为 0dp,这样它就可以占用约束之间的整个可用空间。

android:layout_width="0dp"

我还删除了 insetRightinsetLeft 标签。这是更新后的 XML

<com.google.android.material.button.MaterialButton
        android:id="@+id/backBtn"
        android:layout_width="0dp"
        android:layout_height="59dp"
        android:text="Back"
        android:insetTop="0dp"
        android:insetBottom="0dp"
        app:backgroundTint="#470000"
        app:cornerRadius="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

答案 1 :(得分:0)

试试这个 XML 代码

 <com.google.android.material.button.MaterialButton
        android:id="@+id/backBtn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@drawable/back_btn_bg"
        android:fontFamily="@font/montserrat_thin"
        android:text="@string/back_btn"
        app:backgroundTint="#470000"
        app:cornerRadius="0dp"
        app:fontFamily="@font/montserrat_thin"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />