如何使图像在线性布局中居中?

问题描述 投票:1回答:4

我有一个xml屏幕,在垂直方向的父线性布局中具有三个子线性布局。第一个孩子包含一个图像视图,运行该应用程序时,我看不到任何图像。为什么?图像尺寸太大吗?如果是这样..如何缩放呢?但是,图像可以在我的设计选项卡中查看。 android stdudio。以下是我完整的xml文件代码。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_sign_up"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.root.my_laoder.SignUp"
    android:orientation="vertical">

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

       <ImageView
       android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:adjustViewBounds="true"
    android:scaleType="centerInside"
    android:src="@drawable/ty"
       />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical"
        android:paddingTop="50dp"


        >


        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"

           />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"

            />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="number"

            />
    </LinearLayout>

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingTop="50dp"
        >

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"/>

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"

        />

</LinearLayout>


</LinearLayout>
android xml android-imageview image-scaling
4个回答
3
投票

1。将属性android:gravity="center_horizontal"添加到LinearLayout,以在中心显示ImageView

2。ImageView宽度更新为android:layout_width="wrap_content"并删除属性android:layout_gravity="center"

3。使用android:scaleType="fitXY"代替android:scaleType="centerInside"

尝试一下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_sign_up"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:src="@drawable/ty" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical"
        android:paddingTop="50dp">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="number" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingTop="50dp">

        <Button
            android:text="Button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5" />

        <Button
            android:text="Button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5" />

    </LinearLayout>
</LinearLayout>

输出:

enter image description here


0
投票

这些参数的减光值是多少?

android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"

以及为什么在父布局为线性的情况下也为imageView使用单独的linearlayout?

请将该内容删除

您是否已在ImageView中检查了用作背景的图像尺寸?如果尺寸大于图像可能会将其余视图从屏幕推出,请尝试提供固定的宽度和高度,假设两者均为100dp,看看您是否能够看到任何东西。

如果您想使ImagView视图中的imageView居中水平使用

android:layout_gravity="center_horizontal"

以上代码行将不适用于ImageView的单独线性布局。如果您仍然要为ImageView使用单独的布局,请在该linearlayout内部使用以下代码行

android:gravity =“ center_horizo​​ntal”

注意:对不起,我无法发表评论。


0
投票

为您的ImageView尝试使用此方法:

<ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:adjustViewBounds="true"
            android:scaleType="centerInside"
            android:src="@android:drawable/btn_minus" />

0
投票

我认为您错过了ImageView的scaleType,请按照以下步骤使您的图像居中

 1. Add attribute android:layout_gravity="center_horizontal" to show ImageView in center.

 2. Set ImageView layout_width="match_parent" and 
 layout_height="@dimen/fixed_height"
 ==> height will be fixed but width will automatically adjust 
 according to device size

 3. and set scaleType="fitEnd" or scaleType="fitCenter"
 ==> fitEnd is ( Scale the image from the end of the container ) and 
 possible to have space on top
 if image is different each time.
 ==> fitCenter is (Scale the image from center) and possibility to 
 have space in top and bottom

    Example:
    <ImageView
        android:id="@+id/cover_image"
        android:layout_width="match_parent"
        android:layout_height="@dimen/fixed_height"
        android:layout_gravity="center_horizontal"
        android:scaleType="fitEnd" />
© www.soinside.com 2019 - 2024. All rights reserved.