【问题标题】:Using two spaces to place an object in the middle of the screen in a horizontal linearlayout使用两个空格将对象放置在屏幕中间的水平线性布局中
【发布时间】:2021-05-29 01:45:05
【问题描述】:

我看到了这个链接和 bakwarte 的答案。 Center two buttons horizontally

Toni Gamez 说,这适用于 API >= 14。

例如,我尝试并看到它适用于 10 级。 那么这可能是所有 API 级别的正确方法吗?

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

【问题讨论】:

  • ConstraintLayout 从 API 9 开始可用,使用它可以获得更好的结果。将图像限制在屏幕的左侧和右侧,然后将宽度设置为 100dp 将做同样的事情,而且工作量更少。如果你也想要我,我可以发布一个例子作为答案。
  • @avalerio 也就是不使用空格,不管安卓版本,给空格一样的结果?

标签: android android-linearlayout


【解决方案1】:

Toni Gamez 没有说明为什么答案仅适用于 API >= 14,但可能是因为 Space 小部件是在 API 14 中首次引入的。

如果您从支持库或 Androidx 获取 Space 小部件,此技术将适用于 API 10 和其他 API。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    • 2015-07-10
    • 1970-01-01
    • 1970-01-01
    • 2018-10-20
    • 1970-01-01
    • 2023-03-23
    相关资源
    最近更新 更多