android - 方形按钮屏幕宽度和重量匹配

标签 android xml android-layout button android-layout-weight

我的布局只包含一个 LinearLayour,里面有 3 个按钮。

我希望这 3 个按钮垂直填满屏幕,所以我使用了“weigth=1”,但我还希望按钮是正方形的,也就是说,它们的宽度等于它们的高度。

有什么办法吗?

这是 .xml 文件

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="android.jose.se.Prueba"
    android:background="@drawable/bg"
    android:gravity="center">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">

    <Button
        android:id="@+id/bHet"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@drawable/layer1"
        android:onClick="cambiarBoton"/>

    <Button
        android:id="@+id/bGay"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@drawable/layer2"
        android:onClick="cambiarBoton"/>

    <Button
        android:id="@+id/bLesbi"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@drawable/layer3"
        android:onClick="cambiarBoton"/>

</LinearLayout>
</RelativeLayout>

最佳答案

我遇到了类似的问题并使用 Google's Percent Relative Layout 完成了它帮助您管理 View 纵横比。

你可以这样使用它

 <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:layout_width="match_parent"
         app:layout_aspectRatio="100%"/>
 </android.support.percent.PercentFrameLayout>

关于android - 方形按钮屏幕宽度和重量匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38743929/

相关文章:

android - FCM,Notification类邮件,如何像Gmail一样分组?

Android - 当位置改变时使用监听器从 recyclerview 获取当前位置

java - 更改对话框文本 Android

ios - Swift 解析大量 XML 文件内存不断增长

java - 在 ListView 中编辑文本?

android - 为什么 0dp 被认为是性能增强?

java - logcat 在 Android 中显示无法解释的 libgps 错误

java - 将 XML 解析为 Java 列表

Android KeyCode大写字母?

android - CardView 未显示在 TextView 下方