【问题标题】:Material Button not filling whole width of screen材质按钮未填满屏幕的整个宽度
【发布时间】:2021-08-28 01:02:39
【问题描述】:

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

这是我的组件树:

这是 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>

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

【问题讨论】:

    标签: java android xml material-design


    【解决方案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" />
    

    【讨论】:

    • 不幸的是它没有改变任何东西。按钮左右还有一些空间。
    • @Hubit 去掉 insetLeft 和 insetRight,只保留顶部和底部的 inset 并将宽度设置为 0dp。还有两个字体系列标签。删除其中一个
    • 我完全按照你的描述做了。但它仍然没有一路扩展。我在某处读到,为了不同设备之间的一致性,您应该将字体系列添加到 android 和应用程序。
    • 我建议你做一次新的布局,然后再试一次,你可以添加字体。我尝试复制与您相同的 XML,它对我有用。
    • 谢谢你的工作。我发现我的drawable导致了我指定为背景的问题。由于某种原因,它被分配了填充。再次感谢您的宝贵时间:)
    【解决方案2】:

    试试这个 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" />
    

    【讨论】:

    • android:layout_width使用0dp
    • 这里一样,可惜没有区别。
    猜你喜欢
    • 2021-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-30
    • 2012-04-02
    • 1970-01-01
    • 2018-04-14
    相关资源
    最近更新 更多