【问题标题】:Android : How to clip views by parent, like CSS overflow:hiddenAndroid:如何按父级剪辑视图,例如 CSS 溢出:隐藏
【发布时间】:2018-11-23 23:51:40
【问题描述】:

我的看法如下:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/global_legal_gap"
    android:clipToPadding="true"
    android:clipChildren="true"
    android:baselineAligned="false"
    android:background="@drawable/post_sound_bg"
    android:foreground="?attr/selectableItemBackgroundBorderless"
    android:orientation="horizontal">

    <RelativeLayout
        android:layout_width="@dimen/post_sound_card_height"
        android:layout_height="@dimen/post_sound_card_height">

        <com.facebook.drawee.view.SimpleDraweeView
            android:id="@+id/album_art"
            android:layout_width="@dimen/post_sound_card_height"
            android:layout_height="@dimen/post_sound_card_height"
            fresco:backgroundImage="@drawable/music_placeholder" />

        <RelativeLayout
            android:id="@+id/play_icon_control"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"
            android:visibility="gone">

            <View
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_centerInParent="true"
                android:layout_margin="3dp" />
        </RelativeLayout>
    </RelativeLayout>
<LinearLayout>

如父 RelativeLayout 所示,我使用的是 android:clipToPadding="true"android:clipChildren="true",但是这个父视图的子视图仍然突出在它之外。

或者我这样做对吗?如何实现类似 CSS 的 overflow:hidden

【问题讨论】:

  • 你试过 adjustViewBounds = "true" 吗?
  • @harshjain `adjustViewBounds` 用于图像,此外,它甚至不是这个,而是纵横比。
  • 代码中没有android:clipToPadding。你能张贴你想要的草图吗?
  • @TadijaBagarić 代码只是被格式错误的代码删掉了,请参阅编辑。
  • 您是否尝试将 android:layout_height 设置为 match_parent

标签: java android css xml android-view


【解决方案1】:

考虑更改布局。你想要的可以通过ConstraintLayout来完成。

只需设置布局的尺寸,不要对要溢出/隐藏的部分设置约束。

以下代码显示了一个View,它根据其约束调整其尺寸,另一个溢出。

创建一个新的android项目并将其粘贴为activity_main.xml

<?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"
    tools:context=".MainActivity"
    android:layout_margin="55dp">


    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/guideline"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@android:drawable/sym_def_app_icon"/>

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_begin="84dp"/>

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="0dp"
        android:layout_height="300dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline"
        app:srcCompat="@mipmap/ic_launcher"/>
</android.support.constraint.ConstraintLayout>

【讨论】:

  • 如果我在上面发布的内容可以做到这一点,我会很高兴。在更改任何内容之前,我想了解为什么这不起作用。
【解决方案2】:

你的父视图有

android:layout_width="match_parent"
android:layout_height="wrap_content"

这意味着如果子视图足够大,视图会占用所有可用宽度和所有可用高度。使用此设置,您将看不到 overflow:hidden 行为,因为父级将调整自身大小以包含子级,直到整个屏幕大小。

实际上,android 中的默认视图行为类似于overflow:hidden

您需要做的是在父级上设置固定尺寸。

尝试使用类似的东西:

<LinearLayout
    android:layout_width="20dp"
    android:layout_height="20dp"

你会明白的。

另一方面,您不需要使用 LinearLayout 来托管 RelayiveLayout - 直接使用 RelativeLayout。此外,使用 android:orientation="horizontal" 使其行为类似于 flexbox 方向行,不知道这是否是您想要的。

【讨论】:

    猜你喜欢
    • 2015-02-21
    • 1970-01-01
    • 2018-11-02
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    • 2016-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多