【问题标题】:How to center fit image in linear layout?如何在线性布局中居中拟合图像?
【发布时间】:2017-09-30 04:17:33
【问题描述】:

我有一个 xml 屏幕,在垂直方向的父线性布局中具有三个子线性布局。第一个孩子包含一个图像视图,运行应用程序时我无法查看任何图像。为什么?图片是不是太大了??如果是这样..如何扩展它?但是图像可以在我的设计选项卡中查看。的安卓标准。下面是我的 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>

【问题讨论】:

  • 在您的 ImageView 中为 wrap_content 执行 height and width
  • 我做到了,我也尝试过 scaletype 检查我编辑的代码。没有结果:(
  • 如果您可以提供快照,对我来说会更容易。此外,为了使您的图像居中,您必须将 android:gravity="centre_vertical|centre_horizontal" 放在包含 imageview 的线性布局中
  • 更改图像有效吗?或不同的比例类型(fitCenter)?尝试将 imageView 高度更改为 wrap_content (如果不是,您会将其余视图推出屏幕)
  • imagebin.ca/v/3L6C3tyxYASY @jack jay 我已经向您展示了设计选项卡。我不知道为什么我的移动应用程序显示的是空白而不是这个标志。

标签: android xml android-imageview image-scaling


【解决方案1】:

这些参数的尺寸值是多少?

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 使用单独的线性布局?

请删除

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

如果你想让 imageView 在 ImagView 视图中水平居中使用

android:layout_gravity="center_horizontal"

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

android:gravity="center_horizo​​ntal"

注意:抱歉,我无法发表评论。

【讨论】:

    【解决方案2】:

    为您的 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" />
    

    【讨论】:

    • imagebin.ca/v/3L6C3tyxYASY 这是我的设计选项卡视图.. 但我不知道为什么我的 android 应用程序显示一个空白而不是这个徽标......页面的其余部分很棒......你能告诉??看我甚至用过你的一段代码
    • 尝试给 ImageView 一个固定的高度。
    • 试过了,但什么也没发生,即使这样也行。这不会是一个好习惯。第二件事是我什至尝试过使用不同的图像都工作正常。就是这个神奇的画面
    • 哇。我可以试试我的机器的这张图片吗?
    【解决方案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>
    

    输出:

    【讨论】:

    • 假设这个 ibm 图像是一个大图像.. 会以这种方式居中吗?在屏幕上调整?
    • 我想这解决了问题,但我的应用程序在加载时崩溃说内存不足的问题。我想我的图像太大了。如何处理?你能告诉吗???请
    • 您可以使用 Glide 或 Picasso 来加载大图。或使用小图像作为您的标志,无需使用大图像。我用了 500 * 400 图片
    • Picasso: square.github.io/picasso 和 Glide: github.com/bumptech/glide .... 都非常好用,可以流畅处理大图
    【解决方案4】:

    我想你错过了 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" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-17
      • 1970-01-01
      • 2012-11-08
      • 2013-08-05
      • 1970-01-01
      • 2019-03-03
      • 2012-03-24
      相关资源
      最近更新 更多