Cap*_*emo 5 android android-custom-view android-constraintlayout
我有一个由3个元素组成的自定义视图.我希望视图适应它可用的大小.我希望它看起来像
.
但为了使它看起来像这样我必须添加硬编码大小约束,从而使它无法自适应.
这就是它在所有图像视图上使用match_constraint的样子(不是我想要它的样子
):
这是xml布局.
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<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="in.avimarine.boatangels.activities.InspectionResultActivity"
tools:visibility="visible">
<ImageView
android:id="@+id/moored_boat_bowlines"
android:layout_width="0dp"
android:layout_height="0dp"
android:contentDescription="@string/boat_drawing_content_description"
app:layout_constraintBottom_toTopOf="@+id/moored_boat_body"
app:layout_constraintEnd_toEndOf="@+id/moored_boat_body"
app:layout_constraintStart_toStartOf="@+id/moored_boat_body"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_monohull_mooring_bow"
tools:visibility="visible"/>
<ImageView
android:id="@+id/moored_boat_body"
android:layout_width="0dp"
android:layout_height="0dp"
android:contentDescription="@string/boat_drawing_content_description"
app:layout_constraintBottom_toTopOf="@+id/moored_boat_sternlines"
app:layout_constraintDimensionRatio="h,1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/moored_boat_bowlines"
app:layout_constraintVertical_chainStyle="spread_inside"
app:srcCompat="@drawable/ic_top_mv"
tools:visibility="visible"/>
<ImageView
android:id="@+id/moored_boat_sternlines"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:contentDescription="@string/boat_drawing_content_description"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/moored_boat_body"
app:layout_constraintStart_toStartOf="@+id/moored_boat_body"
app:layout_constraintTop_toBottomOf="@+id/moored_boat_body"
app:srcCompat="@drawable/ic_monohull_dock_aft"
tools:visibility="visible"/>
</android.support.constraint.ConstraintLayout>
</merge>
Run Code Online (Sandbox Code Playgroud)
如何使这种自适应仍然看起来像第一张图像?
您可以使用ConstraintLayout创建自定义自适应视图.请注意layout_constraintVertical_weight属性.
boat.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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/moored_boat_bowlines"
android:layout_width="0dp"
android:layout_height="0dp"
android:src="@drawable/bowlines"
app:layout_constraintBottom_toTopOf="@+id/moored_boat_body"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_weight="1" />
<ImageView
android:id="@+id/moored_boat_body"
android:layout_width="0dp"
android:layout_height="0dp"
android:src="@drawable/body"
app:layout_constraintBottom_toTopOf="@+id/moored_boat_sternlines"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/moored_boat_bowlines"
app:layout_constraintVertical_weight="10" />
<ImageView
android:id="@+id/moored_boat_sternlines"
android:layout_width="0dp"
android:layout_height="0dp"
android:src="@drawable/sternlines"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/moored_boat_body"
app:layout_constraintVertical_weight="1" />
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
然后您可以在任何其他布局中使用它.
activity_one.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#8493"
android:gravity="center"
android:text="Filled!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<include layout="@layout/boat" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
activity_two.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/boat_view"
layout="@layout/boat"
android:layout_width="100dp"
android:layout_height="100dp" />
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
380 次 |
| 最近记录: |