【问题标题】:Trouble centering a bitmap on an ImageView无法在 ImageView 上居中位图
【发布时间】:2011-05-12 05:28:25
【问题描述】:

我在 xml 中有一个 ImageView (image_view),我将布局重力设置为 center,比例设置为 center_inside....但是当我将压缩的 bmp 传递给 image_view 时,它会将它放在中间-屏幕左侧(纵向模式)...关于解决方法的任何想法?

这是我将其设置为图像视图:

        ImageView imageView = (ImageView) findViewById(R.id.image_view);

        imageView.setImageBitmap(bitmap565);

我也在输出流中压缩它:

        try {
            FileOutputStream fos = new FileOutputStream(filepath);

            bitmap565.compress(CompressFormat.JPEG, 90, fos);

            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

谢谢大家!

    <ImageButton android:id="@+id/ImageButton01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/results" android:layout_gravity="top|center"></ImageButton><ImageView
            android:screenOrientation="portrait"
            android:layout_width="fill_parent"
            android:id="@+id/image_view"
            android:layout_gravity="center" android:layout_weight="1.0" android:layout_height="wrap_content" android:scaleType="centerInside"/>

    <Button
            android:screenOrientation="portrait"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal" android:layout_width="fill_parent" android:textStyle="bold" android:id="@+id/detect_face" android:text="Detect"/>

【问题讨论】:

  • 请粘贴 XML 布局。
  • 已编辑 - 很抱歉格式不佳
  • 你能发布完整的布局吗?或者至少,它的所有父母?顺便说一下,ImageViews 没有重力。

标签: android layout imageview center bmp


【解决方案1】:

您只需使用 FrameLayout 即可解决您的问题。

【讨论】:

    【解决方案2】:

    您的ImageViewlayout_width 设置为fill_parent,因此layout_gravity 不会影响其在其父级中的水平位置。尝试使用gravity 代替layout_gravity,或使用wrap_content 代替fill_parent

    【讨论】:

    • 嘿 - 我试过了,但它仍然显示在中心的左侧。我强制应用处于纵向而不是横向 - 你认为这是个问题吗?
    • 这些View的容器是什么?它也应该是 layout_width="fill_parent"。
    猜你喜欢
    • 2011-07-23
    • 1970-01-01
    • 1970-01-01
    • 2017-04-11
    • 1970-01-01
    • 1970-01-01
    • 2020-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多