[英]Material Button not filling whole width of screen
I am pretty new to the whole app development stuff, and I'm just trying to learn the basics.我对整个应用程序开发的东西都很陌生,我只是想学习基础知识。 Especially with XML I only have very little experience.尤其是 XML 我只有很少的经验。 My problem is, that I want to add a button, which expands over the whole width of the screen, but it just won't work and leaves some kind of margin around it.我的问题是,我想添加一个按钮,该按钮在屏幕的整个宽度上展开,但它不起作用并在它周围留下某种边距。 I have already tried a lot, but I couldn't find any solution, or maybe I just didn't know what to exactly search for.我已经尝试了很多,但我找不到任何解决方案,或者我只是不知道到底要搜索什么。 One tip I found with the android:insets attributes didn't work, except for top and bottom insets.我在 android 中发现的一个提示:insets 属性不起作用,除了顶部和底部插入。
This is my component tree:这是我的组件树:
And this is the XML:这是 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>
I hope I included every information that's needed.我希望我包含了所有需要的信息。 Thank you in advance:)先感谢您:)
Just change the layout_width
as 0dp
so that it can take whole available space between the constraints.只需将layout_width
更改为0dp
以便它可以在约束之间占用整个可用空间。
android:layout_width="0dp"
I also removed the insetRight
and insetLeft
tag.我还删除了insetRight
和insetLeft
标签。 Here is the updated XML这是更新的 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" />
Try this XML code试试这个 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" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.