weight的情况下将两个按钮水平划分

weight的情况下将两个按钮水平划分

本文介绍了如何在不使用layout_weight的情况下将两个按钮水平划分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个名为btnBeginner和btnAdvanced的按钮.
我已经通过使用layout_weight属性将这两个Button均分了.但是layout_weight不利于性能.
因此,我想更改现有代码-如下所示.

I have two Buttons called btnBeginner and btnAdvanced.
I have divided these two Buttons equally by using the layout_weight property. But the layout_weight is bad for performance.
Because of that, I would like to change my existing code - which is shown below.

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

请帮助我在不使用layout_weight属性的情况下执行相同的操作.

Please help me to do the same without using the layout_weight property.

推荐答案

PercentRelativeLayout .考虑使用 ConstraintLayout 和关联的布局.

EDIT : PercentRelativeLayout was deprecated in API level 26.1.0.consider using ConstraintLayout and associated layouts instead.

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

Nested weights are bad for performance because :

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

So in your case, weight will not create the performance problem. But still if you want to divide the layout into two equal parts without using weight, you can use 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>

访问此Github存储库以获取更多信息

这篇关于如何在不使用layout_weight的情况下将两个按钮水平划分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 11:30