【问题标题】:Padding or margins is only on left and right side but not on top and bottom填充或边距仅在左侧和右侧,但不在顶部和底部
【发布时间】:2018-12-24 09:59:33
【问题描述】:

当我试图通过在FrameLayout 中使用填充或边距在我的ImageView 周围制作边框(框架)时(在代码中它的 id 为:main_frame),它只显示在左侧和右侧,但不显示在顶部和底部。

但正如我所写,我需要它,因为它应该看起来像框架。

有一个代码:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimaryDark"
    tools:context=".WallActivity">

    <RelativeLayout
        android:id="@+id/wall_frame"
        android:layout_width="592dp"
        android:layout_height="315dp"
        android:background="@android:color/transparent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout"
        app:layout_constraintVertical_bias="1.0">

        <ImageView
            android:id="@+id/wall"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter"
            android:src="@drawable/room"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

        <FrameLayout
            android:id="@+id/main_frame"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="@android:color/transparent"
            android:visibility="visible"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintVertical_bias="0.621"
            android:layout_margin="40dp">

            <FrameLayout
                android:id="@+id/frame"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="@android:color/black"
                android:visibility="visible"
                android:padding="20dp">


                <FrameLayout
                    android:id="@+id/mat"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:background="@android:color/white"
                    android:visibility="visible"
                    android:padding="10dp">

                    <RelativeLayout
                        android:id="@+id/imageholder"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:background="@android:color/transparent">

                        <ImageView
                            android:id="@+id/picture"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:adjustViewBounds="true"
                            android:scaleType="fitCenter"
                            android:src="@drawable/nature"
                            android:visibility="visible"
                            android:layout_centerInParent="true"/>
                    </RelativeLayout>

                </FrameLayout>
            </FrameLayout>
        </FrameLayout>

    </RelativeLayout>
    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="586dp"
        android:layout_height="36dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="4dp"
        android:orientation="horizontal"
        android:weightSum="35.5"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <Button
            android:id="@+id/hideBtn"
            android:layout_width="0dp"
            android:layout_height="30dp"
            android:layout_weight="4"
            android:background="@drawable/menubutton"
            android:text="Hide"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"
            android:textColor="@color/colorGrey"
            android:textSize="16sp" />

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

        <Button
            android:id="@+id/clearBtn"
            android:layout_width="0dp"
            android:layout_height="30dp"
            android:layout_weight="4"
            android:background="@drawable/menubutton"
            android:text="Clear"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"
            android:textColor="@color/colorGrey"
            android:textSize="16sp" />

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

        <Button
            android:id="@+id/lockBtn"
            android:layout_width="0dp"
            android:layout_height="30dp"
            android:layout_weight="4"
            android:background="@drawable/menubutton"
            android:text="Lock"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"
            android:textColor="@color/colorGrey"
            android:textSize="16sp" />

        <Space
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="5" />

        <Button
            android:id="@+id/exportBTN"
            android:layout_width="0dp"
            android:layout_height="30dp"
            android:layout_weight="4"
            android:background="@drawable/menubutton"
            android:text="Export"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"
            android:textColor="@color/colorGrey"
            android:textSize="16sp" />

        <Space
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="5" />


        <Button
            android:id="@+id/wallBtn"
            android:layout_width="0dp"
            android:layout_height="30dp"
            android:layout_weight="4"
            android:background="@drawable/menubutton"
            android:text="Wall"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"
            android:textColor="@color/colorGrey"
            android:textSize="16sp" />

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

        <Button
            android:id="@+id/pictureBtn"
            android:layout_width="0dp"
            android:layout_height="30dp"
            android:layout_weight="4"
            android:background="@drawable/menubutton"
            android:text="Picture"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"
            android:textColor="@color/colorGrey"
            android:textSize="16sp" />

    </LinearLayout>

</android.support.constraint.ConstraintLayout>

这是截图。

您知道如何解决吗?我尝试搜索,但找不到类似的东西。或者有没有更好的制作框架的方法?

谢谢!

//已编辑:

我添加了完整的 xml 代码,向 FrameLayouts 添加了一些填充,id: mat, frame。它看起来很完美,但 main_frame 仍然坏了。 main_frame 应该是不可见的(透明颜色)所以你可能认为它不一定要解决它,但我发现当我让它像我改变它时它破坏了图像一样。它为图像添加了一些白色边框(仅在左侧和右侧)......

还有一张图片告诉你框架看起来不错,但正如我所说的 main_frame 坏了......

【问题讨论】:

  • 请提供完整的 xml(即布局的文本部分)。

标签: android xml margin frame padding


【解决方案1】:

您可以考虑使用RelativeLayoutImageView 进行简单布局。 RelativeLayout 可能有背景,而ImageView 也会有一些填充和背景。所以布局将是这样的。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/room">

    <ImageView
        android:id="@+id/picture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@android:color/black"
        android:padding="16dp"
        android:scaleType="fitCenter"
        android:src="@drawable/nature" />
</RelativeLayout>

希望有帮助!

【讨论】:

  • 我试过了,但看起来一样。使用这个在顶部和底部制作一些“框架”,但它的尺寸仍然小于侧面。不过谢谢。我将添加完整的 xml,正如您所写的,也许我在其他地方出了点问题......
【解决方案2】:

将此:android:padding="2dp" 添加到 ID 为 frame 的 FrameLayout。您可以将 2dp 更改为您喜欢的。

【讨论】:

  • 框架和垫子可以正常工作。只有 main_frame 坏了...但是谢谢! ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-14
  • 2014-07-10
  • 1970-01-01
  • 2015-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多