我有一个布局来显示可绘制的虚线,其宽度为1 do。当前,布局高和宽度正在拉伸页面。但是我只希望它围绕可绘制图像,需要对此进行哪些更改?Current layout
dashes.xml
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/dashedVuew"
android:src="@drawable/dashedLine"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:layout_gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
所以您只需要一个ImageView的布局?没有其他意见?
如果是这样,则根本不需要使用ConstraintLayout!只需将FrameLayout与一个ImageView一起使用,以及layout_gravity =“ center”。
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/dashedVuew"
android:src="@drawable/dashedLine"
android:layout_width="match_parent" // or wrap_content
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</FrameLayout>
P.S。无需在ImageView中定义xmlns!