将 LinearLayout 居中到屏幕,用 ImageView 填充上面的 space
Center LinearLayout to screen, fill space above with ImageView
我正在尝试重新创建这个:
我需要一个以屏幕为中心的 LinearLayout
。 LinearLayout
上方的 space 应该用 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 xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="25dp"
android:paddingRight="25dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/center_layout"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:src="@mipmap/ic_launcher"/>
<LinearLayout
android:id="@+id/center_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="asdasdasdas"
android:textSize="50dp"/>
</LinearLayout>
</RelativeLayout>
你可以试试这个
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="25dp"
android:paddingBottom="25dp"
android:paddingLeft="25dp"
android:paddingRight="25dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_marginBottom="25dp"
android:background="@mipmap/ic_launcher"
android:layout_centerHorizontal="true"
android:scaleType="center"/>
<LinearLayout
android:id="@+id/center_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/imageView"
android:background="@android:color/holo_blue_bright"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:text="TextView inside Linear Layout"
android:textSize="20sp"/>
</LinearLayout>
</RelativeLayout>
这是结果
您需要一个 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 上方的 space。
对 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>
我正在尝试重新创建这个:
我需要一个以屏幕为中心的 LinearLayout
。 LinearLayout
上方的 space 应该用 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 xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="25dp"
android:paddingRight="25dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/center_layout"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:src="@mipmap/ic_launcher"/>
<LinearLayout
android:id="@+id/center_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="asdasdasdas"
android:textSize="50dp"/>
</LinearLayout>
</RelativeLayout>
你可以试试这个
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="25dp"
android:paddingBottom="25dp"
android:paddingLeft="25dp"
android:paddingRight="25dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_marginBottom="25dp"
android:background="@mipmap/ic_launcher"
android:layout_centerHorizontal="true"
android:scaleType="center"/>
<LinearLayout
android:id="@+id/center_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/imageView"
android:background="@android:color/holo_blue_bright"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:text="TextView inside Linear Layout"
android:textSize="20sp"/>
</LinearLayout>
</RelativeLayout>
这是结果
您需要一个 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 上方的 space。
对 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>