Android 使所有按钮具有相同的尺寸 我想在中间做一个有三个按钮的布局。稍后,我将在右上角再添加两个按钮,但目前我正在尝试使用这3个按钮

Android 使所有按钮具有相同的尺寸 我想在中间做一个有三个按钮的布局。稍后,我将在右上角再添加两个按钮,但目前我正在尝试使用这3个按钮,android,android-layout,button,android-relativelayout,Android,Android Layout,Button,Android 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

这就是我的想法:

在某种程度上,它应该在每个屏幕大小上都是相似的(因此按钮会随着屏幕大小的变小而变小)

这就是我所拥有的:

<?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/multiplay"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:background="@drawable/menubutton"
    android:text="@string/multiplayer" />

<Button
    android:id="@+id/options"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/multiplay"
    android:layout_below="@+id/multiplay"
    android:background="@drawable/menubutton"
    android:text="@string/options" />

<Button
    android:id="@+id/singleplay"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/multiplay"
    android:layout_alignLeft="@+id/multiplay"
    android:background="@drawable/menubutton"
    android:text="@string/singleplayer"
 />

在大屏幕上看起来不错,但在小屏幕上会发生这种情况:

我在谷歌上搜索过,大多数页面都建议使用“权重”,但显然这只适用于线性布局。有什么建议吗


谢谢

您应该使用权重,这样无论屏幕大小如何,您都可以均匀地分配屏幕上的按钮

这里是一个在相对论框架内的线性布局示例。(如果RelativeLayout中没有其他内容,则可以使用LinearLayout替换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">

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="3" >

<Button
    android:id="@+id/multiplay"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:layout_centerHorizontal="true"
    android:background="@drawable/menubutton"
    android:text="@string/multiplayer" />

<Button
    android:id="@+id/options"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:layout_centerHorizontal="true"
    android:background="@drawable/menubutton"
    android:text="@string/options" />

<Button
    android:id="@+id/singleplay"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:layout_centerHorizontal="true"
    android:background="@drawable/menubutton"
    android:text="@string/singleplayer"
 />

    </LinearLayout>

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

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical" >

        <Button
            android:id="@+id/multiplay"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/menubutton"
            android:text="@string/multiplayer" />

        <Button
            android:id="@+id/options"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/menubutton"
            android:text="@string/options" />

        <Button
            android:id="@+id/singleplay"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/menubutton"
            android:text="@string/singleplayer" />
    </LinearLayout>

</RelativeLayout>


您不使用线性布局的原因是什么?使用线性布局更好吗?考虑到您只想将3个按钮放在彼此上方,是的,使用线性布局更好。您仍然可以使用RealValayOUT作为总体布局(稍后您可以在右上角添加这些按钮),但是在中间使用3个按钮的线性布局。您可以使用线性布局,或者使用<代码> TabelayayOuts<代码>三代码> TabLeRoW >其中每个<代码> TabLeRoW包含<代码>按钮< /代码>。您的选择。是的,我建议您使用线性布局并使用权重功能。这是一个很好的例子。