将LinearLayout居中到屏幕,用ImageView填充上面的空间

时间:2015-09-22 01:23:07

标签: android android-layout android-xml

我试图重新创建这个:

enter image description here

我需要一个以屏幕为中心的LinearLayout。应使用LinearLayout填写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>

3 个答案:

答案 0 :(得分:1)

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

<body ng-controller="MainCtrl">
  <div class="row" style="flex-wrap : wrap;">
    <div class="col-33" ng-repeat="item in collection">
      {{item}}
    </div>
  </div>
</body>

确保LinearLayout位于屏幕的中心。

android:layout_centerInParent="true"

确保ImageView位于LinearLayout之上且以水平为中心。

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

确保缩放图像以填充LinearLayout上方的空格而不更改宽高比。

使用android:src代替android:background for ImageView。

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

enter image description here

答案 1 :(得分:0)

请尝试以下代码。

#include <stdio.h>
#include "cent_convert.h"

int main (void)
{
    double fahr;
    fahr = 83.0;
    printf ("%3.1f degrees Fahrenheit is %3.1f degrees centigrade.\n",
        fahr, convert_to_cent(fahr));

    return 0;
}


/*
    // Compile
    gcc -Wall -Wextra -Ofast cent_convert.c -o bin/f2c f2c.c

    // Run
    $ ./bin/f2c
    output from cent_convert: fahr passed in: 83.0
    output from cent_convert: calculated value for cent is: 28.3
    83.0 degrees Fahrenheit is 28.3 degrees centigrade.

*/

答案 2 :(得分:0)

你可以试试这个

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

这是结果

enter image description here