gpt4 book ai didi

android - 如何使按钮居中并且其宽度是容器的百分比?

转载 作者:行者123 更新时间:2023-11-29 15:00:05 39 4
gpt4 key购买 nike

我正在 Android Studio 中制作一个应用程序,我需要一个位于手机屏幕中央的按钮。

我看过很多关于如何让按钮居中的例子,但除此之外,一旦居中,按钮的宽度必须是屏幕宽度的 60%。

欢迎任何想法、建议或示例。

谢谢

最佳答案

@MishaAkopov 的回答很好,但我会提供一个使用线性布局的单独解决方案。

通过将布局的权重和设置为 10,将按钮的权重设置为 6,按钮将占布局宽度的 60%。此外,您需要将 LinearLayout 的重力设置为 center 或 center_horizo​​ntal 以确保按钮居中。

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:weightSum="10">

<Button
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="6"
android:text="My Button"/>

</LinearLayout>

关于android - 如何使按钮居中并且其宽度是容器的百分比?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48065139/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com