如何在不使用layout_weight的情况下将两个按钮水平划分? [英] How to equally divide two buttons horizontally without using layout_weight?

查看:139
本文介绍了如何在不使用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 :

布局权重要求对小部件进行两次测量.当一个 具有非零权重的LinearLayout嵌套在另一个内部 权重非零的LinearLayout,然后是测量数量 呈指数增长.

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.

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

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的情况下将两个按钮水平划分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆