android - 将 LinearLayout 置于屏幕中心,用 ImageView 填充上面的空间

标签 android android-layout android-xml

我正在尝试重新创建这个:

enter image description here

我需要一个以屏幕为中心的 LinearLayoutLinearLayout 上方的空间应该用 ImageView 填充(除了一些填充)。

例如,如果屏幕小于上图中的屏幕,ImageView 应相应调整大小,如:

enter image description here

我该怎么做?

这是我创建的一个模板,让我自己开始:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="25dp"
    android:paddingRight="25dp">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:background="@drawable/logo" />

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

        ...

    </LinearLayout>

</LinearLayout>

最佳答案

您需要一个 RelativeLayout 才能将 LinearLayout 定位在屏幕中央,图像位于其上方。

android:layout_centerInParent="true"

确保 LinearLayout 位于屏幕中央。

android:layout_above="@id/linearLayout"
android:layout_centerHorizontal="true"

确保 ImageView 位于 LinearLayout 上方并水平居中。

android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"

确保缩放图像以填充 LinearLayout 上方的空间而不改变纵横比。

为 ImageView 使用 android:src 而不是 android:background。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="25dp"
    android:paddingRight="25dp">

    <LinearLayout
        android:orientation="vertical"
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/>

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitCenter"
        android:layout_above="@id/linearLayout"
        android:layout_centerHorizontal="true"
        android:src="@mipmap/ic_launcher" />

</RelativeLayout>

enter image description here

关于android - 将 LinearLayout 置于屏幕中心,用 ImageView 填充上面的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32706949/

相关文章:

Android:GridView 宽度没有包装到内容?

android - Material UI Layout 按角线划分

android - 如何使用属性 android :fontStyle ="bold" 在 font_family.xml 中设置自定义字体

android - 如何使用复合可绘制对象而不是包含 ImageView 和 TextView 的 LinearLayout

android - 按下向上按钮时的自定义 Activity 转换

android - 如何设置布局 :width of an object to (wrap_content + 10)?

android - 如何在不编程的情况下使用可绘制对象创建自定义进度条?

java - DataInputStream/DataOutputStream 类和 InputStream/OutputStream 类的区别

android - 从手机获取所有文件的最佳方式?

java - 如何在Android应用程序中获取设备存储中的所有视频