java - Android:如何按父级剪辑视图,例如 CSS 溢出:隐藏
问题描述
我的看法如下:
<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>
如 parent 所示RelativeLayout
,我正在使用android:clipToPadding="true"
and
android:clipChildren="true"
,但是这个父视图的子视图仍然突出在它之外。
或者我这样做对吗?我如何实现类似 CSS 的东西overflow:hidden
?
解决方案
考虑改变布局。你想要的可以用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>
推荐阅读
- angular - Angular 2,primeng:在 *ngFor 中的 Primeng 中嵌套 *ngFor 和 ngModel 的两种方式绑定
- jqgrid - jqgrid从单元格值打开链接
- angular - 导航时刷新继承的组件
- android - 在 Android 的导航架构组件中将复杂对象作为参数传递
- javascript - 如何将 PDF blob 发送到 MVC 控制器
- python - 在蓝图 Flask 中获取 mongo 实例的正确方法
- c# - 如何获取相对于任何形式的 UIElement 的光标位置
- ios - React Native:如何调试发布版本?
- angular - 为什么从 html 中的 typescript 代码触发和模拟按钮单击不起作用?
- excel - 使用 VBA 将数据导出为双精度