首页 > 解决方案 > 响应式布局 - 如何?

问题描述

我正在尝试使用 Android Studio 开发一个应用程序,但我仍然没有弄清楚如何使这个布局响应:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <Button
                android:id="@+id/b1"
                android:layout_width="0dp"
                android:layout_height="250dp"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="#093A3E"
                android:foreground="?attr/selectableItemBackground"
                android:drawableTop="@drawable/home"
                android:paddingTop="25dp"
                android:text="One"
                android:textAllCaps="false"
                android:textColor="#fff"
                android:textSize="18sp" />
            <Button
                android:id="@+id/b2"
                android:layout_width="0dp"
                android:layout_height="250dp"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="#3AAFB9"
                android:foreground="?attr/selectableItemBackground"
                android:drawableTop="@drawable/home"
                android:paddingTop="25dp"
                android:text="Two"
                android:textAllCaps="false"
                android:textColor="#fff"
                android:textSize="18sp" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <Button
                android:id="@+id/b6"
                android:layout_width="0dp"
                android:layout_height="250dp"
                android:layout_margin="2dp"
                android:foreground="?attr/selectableItemBackground"
                android:layout_weight="1"
                android:background="#64E9EE"
                android:drawableTop="@drawable/home"
                android:paddingTop="40dp"
                android:text="Three"
                android:textAllCaps="false"
                android:textColor="#fff"
                android:textSize="18sp" />
            <Button
                android:id="@+id/b7"
                android:layout_width="0dp"
                android:layout_height="250dp"
                android:foreground="?attr/selectableItemBackground"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="#97C8EB"
                android:drawableTop="@drawable/home"
                android:paddingTop="40dp"
                android:text="Four"
                android:textAllCaps="false"
                android:textColor="#fff"
                android:textSize="18sp" />
        </LinearLayout>
    </LinearLayout>
</ScrollView>

这是我在模拟器上运行应用程序时得到的结果:

在此处输入图像描述

基本上我希望按钮的高度在每个 android 设备上都能响应,所以它们会一直延伸到底部,但我认为我不能只更改每个按钮的“layout_height”。

有没有办法做到这一点?谢谢。

标签: androidandroid-layoutresponsive

解决方案


您还必须android:layout_weightLinearLayout 这样添加:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">
        <Button
            android:id="@+id/b1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:background="#093A3E"
            android:foreground="?attr/selectableItemBackground"
            android:drawableTop="@drawable/home"
            android:paddingTop="25dp"
            android:text="One"
            android:textAllCaps="false"
            android:textColor="#fff"
            android:textSize="18sp" />
        <Button
            android:id="@+id/b2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:background="#3AAFB9"
            android:foreground="?attr/selectableItemBackground"
            android:drawableTop="@drawable/home"
            android:paddingTop="25dp"
            android:text="Two"
            android:textAllCaps="false"
            android:textColor="#fff"
            android:textSize="18sp" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">
        <Button
            android:id="@+id/b6"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:foreground="?attr/selectableItemBackground"
            android:layout_weight="1"
            android:background="#64E9EE"
            android:drawableTop="@drawable/home"
            android:paddingTop="40dp"
            android:text="Three"
            android:textAllCaps="false"
            android:textColor="#fff"
            android:textSize="18sp" />
        <Button
            android:id="@+id/b7"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:foreground="?attr/selectableItemBackground"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:background="#97C8EB"
            android:drawableTop="@drawable/home"
            android:paddingTop="40dp"
            android:text="Four"
            android:textAllCaps="false"
            android:textColor="#fff"
            android:textSize="18sp" />
    </LinearLayout>
</LinearLayout>

推荐阅读