【问题标题】:Best way to make a 3x3 image button layout for android studio为 android studio 制作 3x3 图像按钮布局的最佳方法
【发布时间】:2016-08-29 03:04:07
【问题描述】:

我正在寻找一个 Android 版本(我刚开始学习如何使用 Android Studio)。我在布局上有困难。我想在我的主要活动上制作一个 3x3 图像按钮,但我不知道如何修复它们的大小以使其适合所有屏幕。在 Xcode (iOS) 中,我可以轻松地拖动和调整大小并设置约束,但在 Android Studio 中我无法做到这一点。

谁能告诉我怎么做?或者至少将我重定向到可以帮助我的另一个帖子或视频?

【问题讨论】:

标签: java android android-layout android-studio


【解决方案1】:

您可以使用 GridView,也可以像这样使用 LinearLayout 来实现它。 看看这个 xml 布局,它会为你工作。

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#d3d3d3"
tools:context=".ui.MainActivity">

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary" />

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_margin="2.5dp"
        android:orientation="horizontal"
        android:weightSum="3">

        <LinearLayout
            android:id="@+id/l1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="5dp"
            android:background="@android:color/white"
            android:orientation="vertical"
            >
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitCenter"
                android:src="@drawable/hath_back_icon" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/l2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/white"
            android:layout_margin="5dp"
            android:orientation="vertical"
            >
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitCenter"
                android:src="@drawable/hath_back_icon" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/l3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="5dp"
            android:background="@android:color/white"
            android:orientation="vertical"
            >
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitCenter"
                android:src="@drawable/hath_back_icon" />
        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_margin="2.5dp"
        android:weightSum="3">

        <LinearLayout
            android:id="@+id/l4"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="5dp"
            android:background="@android:color/white"
            android:orientation="vertical"
            >
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitCenter"
                android:src="@drawable/hath_back_icon" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/l5"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/white"
            android:layout_margin="5dp"
            android:orientation="vertical"
            >
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitCenter"
                android:src="@drawable/hath_back_icon" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/l6"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="5dp"
            android:background="@android:color/white"
            android:orientation="vertical"
            >
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitCenter"
                android:src="@drawable/hath_back_icon" />
        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_margin="2.5dp"
        android:weightSum="3">

        <LinearLayout
            android:id="@+id/l7"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="5dp"
            android:background="@android:color/white"
            android:orientation="vertical"
            >
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitCenter"
                android:src="@drawable/hath_back_icon" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/l8"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/white"
            android:layout_margin="5dp"
            android:orientation="vertical"
            >
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitCenter"
                android:src="@drawable/hath_back_icon" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/l9"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="5dp"
            android:background="@android:color/white"
            android:orientation="vertical"
            >
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitCenter"
                android:src="@drawable/hath_back_icon" />
        </LinearLayout>
    </LinearLayout>


</LinearLayout>

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2021-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-06
  • 1970-01-01
相关资源
最近更新 更多