我正在尝试重新创建这个:
我需要一个以屏幕为中心的 LinearLayout
。 LinearLayout
上方的空间应该用 ImageView
填充(除了一些填充)。
例如,如果屏幕小于上图中的屏幕,ImageView
应相应调整大小,如:
我该怎么做?
这是我创建的一个模板,让我自己开始:
<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>
关于android - 将 LinearLayout 置于屏幕中心,用 ImageView 填充上面的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32706949/