填充或边距仅在左侧和右侧,而不在顶部和底部

时间:2018-07-16 19:26:32

标签: android xml margin frame padding

当我尝试通过使用ImageView中的填充或边距(在代码中具有ID:FrameLayout)在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>

这是屏幕截图。

enter image description here

您知道如何解决吗?我尝试搜索,但找不到类似的内容。还是有更好的框架制作方法?

谢谢!

//已编辑:

我添加了完整的xml代码,并向ID为:mat,frame的FrameLayouts添加了一些填充。看起来很完美,但main_frame仍然损坏。 main_frame应该是不可见的(具有透明的颜色),因此您可能认为不一定要解决它,但是我发现当我让它看起来像当我更改它会破坏图像时。它将在图像上添加一些白色边框(仅在左侧和右侧)...

还有另一幅图片向您展示框架看起来不错,但是正如我说的main_frame坏了...

enter image description here

2 个答案:

答案 0 :(得分:0)

添加以下内容:android:padding="2dp"到ID为frame的FrameLayout中。您可以将2dp更改为自己喜欢的格式。

答案 1 :(得分:0)

您可以考虑使用一个RelativeLayout和一个ImageView进行简单布局。 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>

希望有帮助!

相关问题