android - 响应式布局 - 如何?
问题描述
我正在尝试使用 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”。
有没有办法做到这一点?谢谢。
解决方案
您还必须android:layout_weight
像LinearLayout
这样添加:
<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>
推荐阅读
- c# - 你如何在 Unity 的客户端上实例化一个游戏对象?
- android - React Native:FlatList渲染项目之间的空白
- swift - 导航栏徽标标题未居中
- visual-studio-code - vscode - 调试完成后关闭调试控制台
- apache-kafka - KTable 到 KTable 内部连接不会产生所有预期的结果(可能的共同分区问题)
- python - 在没有循环的情况下加入每个 x 元素的数组 - python
- python-2.7 - 在 django 中使用多个数据库
- typescript - 在 Loopback 4 中使用服务
- reactjs - 自定义反应钩子的奇怪行为
- python - 使用默认值验证 jsonschema,其中输入可以是对象和字符串