Android 使子视图比其父视图更宽
我有一个相对论,里面有一个图像视图。 RelativeLayout是一个ListView行,它设置了填充(空间位于ListView的边缘与其子RelativeLayout视图的边缘之间) 我试图使RelativeLayout中的ImageView比使用负边距的行宽。 设置负边距和禁用剪裁(一直向上)。 似乎什么都不管用 代码示例(RelativeLayout在其边缘到屏幕边缘之间具有父级和空格):Android 使子视图比其父视图更宽,android,android-layout,Android,Android Layout,我有一个相对论,里面有一个图像视图。 RelativeLayout是一个ListView行,它设置了填充(空间位于ListView的边缘与其子RelativeLayout视图的边缘之间) 我试图使RelativeLayout中的ImageView比使用负边距的行宽。 设置负边距和禁用剪裁(一直向上)。 似乎什么都不管用 代码示例(RelativeLayout在其边缘到屏幕边缘之间具有父级和空格): 试试这样的方法(代码来自我正在从事的一个项目): 似乎clipping=false在Rela
试试这样的方法(代码来自我正在从事的一个项目):
似乎clipping=false在RelativeLayout中并不能很好地工作,所以使用带有子图像视图的线性布局。对于我来说,我使用了
androidx.constraintlayout.widget.constraintlayout
,并设置android:clipToPadding=“false”
,然后将子图像设置为负边距,然后它就可以工作了
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingStart="@dimen/dp_16"
android:paddingEnd="@dimen/dp_16">
<View
android:id="@+id/gap"
android:layout_marginStart="-16dp"
android:layout_marginEnd="-16dp"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
我不太理解您的要求,但根据我的理解,请尝试一下:因为您的ImageView
是与android:layout\u width=“match\u parent”相对应的的孩子。
您所做的一切都行不通。Tyr将margin
添加到RelativeLayout
中。否则,您可以将静态layout\u width
指定给RelativeLayout
和ImageView
如果ImageView
的宽度大于RelativeLayout的宽度,我必须使用RelativeLayout,整体布局对于线性布局来说太复杂了。我不能用绝对值来表示宽度和高度。“clipping=false在RelativeLayout中不起作用”这个信息节省了我很多时间。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipChildren="false">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
tools:ignore="UselessParent"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="@drawable/dialog_heading_top_line"
android:clipChildren="false"
android:orientation="horizontal"
android:gravity="center_horizontal">
<ImageView
android:id="@+id/default_header_logo"
android:layout_width="167dp"
android:layout_height="72dp"
android:scaleType="center"
android:src="@drawable/default_header_logo"
android:contentDescription="@string/default_heading_logo" />
</LinearLayout>
</RelativeLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingStart="@dimen/dp_16"
android:paddingEnd="@dimen/dp_16">
<View
android:id="@+id/gap"
android:layout_marginStart="-16dp"
android:layout_marginEnd="-16dp"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>