android - Using two spaces to place an object in the middle of the screen in a horizontal linearlayout
问题描述
I saw this link and answer of bakwarte. Center two buttons horizontally
Toni Gamez said, This is for API >= 14.
I tried and saw that it works for level 10, for example. So could this be the right way for all API levels?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="10dp">
<Space
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
<ImageView
android:id="@+id/imageView1"
android:background="@drawable/Icon"
android:layout_width="100dp"
android:layout_height="wrap_content" />
<Space
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
解决方案
Toni Gamez 没有说明为什么答案只适用于 API >= 14,但可能是因为Space小部件是在 API 14 中首次引入的。
如果您从支持库或 Androidx 中获取Space小部件,此技术将适用于 API 10 和其他 API 。
推荐阅读
- bash - 如何使用终端批量复制目录中的文件夹名称?
- arrays - 通过数组求和合并
- apache-kafka - librdkafka partition.assignment.strategy 添加新策略
- visual-studio - VS2019 自定义模板未显示
- java - 如何在 spring 和 vertx 应用程序中维护每个请求的作者姓名?
- angular - 在 iframe、Angular-8 中显示本地 HTML
- scala - 使用 zeebe 输入/输出映射发送 jsonPath
- powershell - Powershell - 如何检查特定机器中的登录用户
- javascript - window.scrollto 和 scrollIntoView 在 ios 中不能平滑滚动
- azure-active-directory - 将来宾用户添加到 Microsoft Planner