gpt4 book ai didi

android - 如何在不使用 layout_weight 的情况下水平平均划分两个按钮?

转载 作者:行者123 更新时间:2023-11-29 14:26:47 25 4
gpt4 key购买 nike

我有两个按钮,分别叫做 btnBeginner 和 btnAdvanced。
我使用 layout_weight 属性将这两个 Button 平均分配。但是 layout_weight 对性能不利。
因此,我想更改现有代码 - 如下所示。

  <LinearLayout
android:id="@+id/lLayoutBeginAdv"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<Button
android:id="@+id/btnBeginner"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".5"
android:background="@color/color_exam_btn_hlight"
android:text="beginner"
android:textAllCaps="false"
android:textColor="@color/white"
android:textStyle="normal" />

<Button
android:id="@+id/btnAdvanced"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight=".5"
android:background="@color/color_exam_btn_normal"
android:text="advanced"
android:textAllCaps="false"
android:textColor="@color/white"
android:textStyle="normal" />

</LinearLayout>

enter image description here

请帮助我在不使用 layout_weight 属性的情况下做同样的事情。

最佳答案

编辑:PercentRelativeLayout在 API 级别 26.1.0 中已弃用。考虑使用 ConstraintLayout和关联的布局。

嵌套权重不利于性能,因为:

Layout weights require a widget to be measured twice. When a LinearLayout with non-zero weights is nested inside another LinearLayout with non-zero weights, then the number of measurements increase exponentially.

因此在您的情况下,重量不会造成性能问题。但是如果你想在不使用重量的情况下将布局分成两个相等的部分,你可以使用 PercentRelativeLayout

示例:

   android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/btnBeginner"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/color_exam_btn_hlight"
android:text="beginner"
android:textAllCaps="false"
android:textColor="@color/white"
android:textStyle="normal"
app:layout_heightPercent="50%"
app:layout_widthPercent="50%"
/>

<Button
android:id="@+id/btnAdvanced"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/color_exam_btn_normal"
android:text="advanced"
android:textAllCaps="false"
android:textColor="@color/white"
android:textStyle="normal"
app:layout_heightPercent="50%"
app:layout_widthPercent="50%"/>

</android.support.percent.PercentRelativeLayout>

访问this Github repo了解更多信息

关于android - 如何在不使用 layout_weight 的情况下水平平均划分两个按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37560852/

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