如何调整这些显示在网格布局上的图像?

How do I adjust these images displayed on gridlayout?

我在网格布局上显示了 4 个方向按钮,试图制作 "game control panel" 这样的:

游戏已经制作完成,但问题在于 "game panel",在平板电脑等大型设备上看起来很小:

如何让按钮显示在父级中?我的 xml 代码是这样的,我制作了一个名为方形布局的自定义布局来显示游戏:

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

tools:context=".MainActivity"
android:background="@drawable/background"
android:padding="10dp">

</*packages*/SquareLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/square"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">

    //gameview
<//*packages*/SquareLayout>


<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/afgergg"
    android:id="@+id/frameLayout"
    android:layout_alignParentBottom="true">


    <GridLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center">

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/up"
            android:layout_row="0"
            android:layout_column="1"
            android:longClickable="true"
            android:background="@drawable/background"
            android:src="@drawable/ic_arrow_up" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/down"
            android:layout_row="2"
            android:layout_column="1"
            android:background="@drawable/background"
            android:src="@drawable/ic_arrow_down" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/left"
            android:layout_row="1"
            android:layout_column="0"
            android:background="@drawable/background"
            android:src="@drawable/ic_arrow_left" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/right"
            android:layout_row="1"
            android:layout_column="2"
            android:background="@drawable/background"
            android:clickable="true"
            android:src="@drawable/ic_arrow_right" />
        </GridLayout>
</FrameLayout>

您可以轻松地将其视为具有 5 个虚拟视图的 3x3 网格 即用

替换您的网格布局
<LinearLayout  android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
    <LinearLayout  android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <View  android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"/>
        <ImageButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/up"
            android:layout_weight="1"
            android:longClickable="true"
            android:adjustViewBounds="true"
            android:background="@drawable/background"
            android:src="@drawable/ic_arrow_up" />
        <View  android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"/>
    </LinearLayout>

    <LinearLayout  android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <ImageButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/left"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:background="@drawable/background"
            android:src="@drawable/ic_arrow_left" />
        <View  android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"/>
        <ImageButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/right"
            android:layout_weight="1"
            android:background="@drawable/background"
            android:clickable="true"
            android:adjustViewBounds="true"
            android:src="@drawable/ic_arrow_right" />
    </LinearLayout>

    <LinearLayout  android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <View  android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"/>
        <ImageButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/down"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:background="@drawable/background"
            android:src="@drawable/ic_arrow_down" />
        <View  android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>

或者您可以使用 table 布局。注意:您可能想添加边距等。

您可以设置 GridLayout 的 children 的宽度和高度,例如 this.It 在每个设备上的大小都相同。

   int mDesiredWidth = 70; 
   int mDesiredHeight= 70; 
   for (int i = 0; i < mGridLayout.getChildCount(); i++) {
        GridLayout.LayoutParams params = (GridLayout.LayoutParams) mGridLayout
                .getChildAt(i).getLayoutParams();
        params.setGravity(Gravity.CENTER);
        params.setMargins(0, 0, 0, 0);
        params.width = Math.round (mDesiredWidth* getResources().getDisplayMetrics().density);
        params.height = Math.round  (mDesiredHeight* getResources().getDisplayMetrics().density);

    }