无论使用何种设备,Android按钮都可以在屏幕上匹配

无论使用何种设备,Android按钮都可以在屏幕上匹配,android,android-layout,button,Android,Android Layout,Button,我开始编写我的第一个android应用程序,并添加了一些按钮 我需要一个横幅按钮,上面和底部都是我管理的,然后是4个按钮,在相同大小的中间填充剩余的空间。 有没有办法做到这一点,而不只是尝试和错误的大小,使它在任何设备上工作 这是我的密码- <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

我开始编写我的第一个android应用程序,并添加了一些按钮

我需要一个横幅按钮,上面和底部都是我管理的,然后是4个按钮,在相同大小的中间填充剩余的空间。 有没有办法做到这一点,而不只是尝试和错误的大小,使它在任何设备上工作

这是我的密码-

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


    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:text="Button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:id="@+id/button5"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:id="@+id/button2"
        android:layout_width="650px"
        android:layout_height="1000px"
        android:layout_above="@+id/button5"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:text="Button"
        android:layout_width="650px"
        android:layout_height="1000px"
        android:layout_below="@+id/button1"
        android:layout_alignRight="@+id/button2"
        android:layout_alignEnd="@+id/button2"
        android:id="@+id/button6" />

    <Button
        android:text="Button"
        android:layout_width="650px"
        android:layout_height="1000px"
        android:layout_alignBottom="@+id/button6"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:id="@+id/button7" />

    <Button
        android:text="Button"
        android:layout_width="650px"
        android:layout_height="1000px"
        android:layout_alignTop="@+id/button2"
        android:layout_alignLeft="@+id/button7"
        android:layout_alignStart="@+id/button7"
        android:id="@+id/button8" />

    </RelativeLayout>

不使用任何库,您就可以实现这一点。顶部按钮和按钮之间的空间将被平均分割为4个(并非始终是正方形,取决于屏幕大小)


不使用任何库,您就可以实现这一点。顶部按钮和按钮之间的空间将被平均分割为4个(并非始终是正方形,取决于屏幕大小)


以下是一个没有嵌套布局权重属性的示例:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/top_button"
        android:layout_width="match_parent"
        android:layout_height="40dp" />

    <Button
        android:id="@+id/bottom_button"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignParentBottom="true"/>

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/top_button"
        android:layout_above="@id/bottom_button"
        android:useDefaultMargins="true"
        android:columnCount="2">
        <Button
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"/>
        <Button
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"/>
        <Button
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"/>
        <Button
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"/>
    </GridLayout>
</RelativeLayout>

以下是一个没有嵌套布局权重属性的示例:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/top_button"
        android:layout_width="match_parent"
        android:layout_height="40dp" />

    <Button
        android:id="@+id/bottom_button"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignParentBottom="true"/>

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/top_button"
        android:layout_above="@id/bottom_button"
        android:useDefaultMargins="true"
        android:columnCount="2">
        <Button
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"/>
        <Button
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"/>
        <Button
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"/>
        <Button
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"/>
    </GridLayout>
</RelativeLayout>


方形按钮?大多数设备都比宽的高,所以在大多数情况下你会得到矩形,但你可以在Android Studio中一次预览所有屏幕大小。不需要运行模拟器嗨,对不起,是的,我是说矩形。很高兴知道!模拟器杀死了我的笔记本电脑。那么,RelativeLayout不应该是你想要的。线性布局对于父级,垂直3行,为这些行添加权重以调整大小,那么中间一行是由4个按钮组成的网格布局正方形按钮?大多数设备都比宽的高,所以在大多数情况下你会得到矩形,但你可以在Android Studio中一次预览所有屏幕大小。不需要运行模拟器嗨,对不起,是的,我是说矩形。很高兴知道!模拟器杀死了我的笔记本电脑。那么,RelativeLayout不应该是你想要的。线性布局对于父级,垂直3行,为这些行添加权重以调整大小,然后中间一行是4个按钮的网格布局,这非常有效。如果我想使顶部和底部的横幅按钮稍微大一点,我需要更改什么?@benjano只需将高度属性更改为某个dp替换wrap_content.Hi,这非常有效。如果我想使顶部和底部的横幅按钮稍微大一点,我需要更改什么?@benjano只需将“高度”属性更改为一些dp替换wrap_内容。