可调自定义 UI
Adjustable custom UI
我有一个由 3 个元素组成的自定义视图。
我希望视图能够适应它可用的大小。
我希望它看起来像 .
但为了让它看起来像这样,我必须添加硬编码大小限制,从而使其 none 自适应。
这是在所有图像视图上使用 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>
如何使这个自适应并看起来仍然像第一张图片?
您可以使用 RelativeLayout.
而不是 ConstraintLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="@drawable/first" />
<ImageView
android:id="@+id/sec"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/first"
android:layout_centerHorizontal="true"
android:src="@drawable/sec" />
<ImageView
android:id="@+id/third"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/sec"
android:layout_centerHorizontal="true"
android:src="@drawable/third" />
</RelativeLayout>
您可以使用 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>
然后您可以在任何其他布局中使用它。
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>
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>
我有办法完成这项工作。包括 android.support.constraint.Guideline
现在您可以将所有三个元素排列在屏幕中央,一个在另一个下方,如图所示。
由于我使用的图像很小,所以它看起来很小并且贴在屏幕顶部。但在您的情况下,这看起来符合预期。所有图像都使用 height
和 width
作为 wrap_content
对齐屏幕顶部和中心
下面是 xml 相同的
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/left_guideline"
app:layout_constraintGuide_percent=".02"
android:orientation="vertical"
/>
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/right_guideline"
app:layout_constraintGuide_percent=".98"
android:orientation="vertical"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/profile"
android:id="@+id/iconOne"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="@+id/left_guideline"
app:layout_constraintRight_toRightOf="@+id/right_guideline"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iconTwo"
android:src="@drawable/contactus"
app:layout_constraintTop_toBottomOf="@id/iconOne"
app:layout_constraintLeft_toLeftOf="@+id/left_guideline"
app:layout_constraintRight_toRightOf="@+id/right_guideline"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iconThree"
android:src="@drawable/pricelist"
app:layout_constraintTop_toBottomOf="@id/iconTwo"
app:layout_constraintLeft_toLeftOf="@+id/left_guideline"
app:layout_constraintRight_toRightOf="@+id/right_guideline"
/>
</android.support.constraint.ConstraintLayout>
将中心与屏幕对齐
现在如果你想让这三张图片居中显示,那么下面的方法就可以了。
对应的xml会是
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/left_guideline"
app:layout_constraintGuide_percent=".02"
android:orientation="vertical"
/>
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/right_guideline"
app:layout_constraintGuide_percent=".98"
android:orientation="vertical"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/profile"
android:id="@+id/iconOne"
app:layout_constraintBottom_toTopOf="@id/iconTwo"
app:layout_constraintLeft_toLeftOf="@+id/left_guideline"
app:layout_constraintRight_toRightOf="@+id/right_guideline"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iconTwo"
android:src="@drawable/contactus"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iconThree"
android:src="@drawable/pricelist"
app:layout_constraintTop_toBottomOf="@id/iconTwo"
app:layout_constraintLeft_toLeftOf="@+id/left_guideline"
app:layout_constraintRight_toRightOf="@+id/right_guideline"
/>
</android.support.constraint.ConstraintLayout>
这些示例将为我们提供 ConstraintLayout 的良好开端
您可以使用 Linearlayout
来解决这个问题,您可以为每张图片设置权重,使其正确对齐并适合所有屏幕尺寸
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:padding="10dp"
android:src="@drawable/top_image"
android:layout_weight="1"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:padding="10dp"
android:layout_marginTop="-100dp"
android:src="@drawable/middle_image"
android:layout_weight="1"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:padding="10dp"
android:layout_marginTop="-100dp"
android:src="@drawable/bottom_image"
android:layout_weight="1" />
</LinearLayout>
欢迎编辑
android:padding="10dp"
android:layout_marginTop="-100dp"
android:src="@drawable/bottom_image"
android:layout_weight="1"
我有一个由 3 个元素组成的自定义视图。
我希望视图能够适应它可用的大小。
我希望它看起来像
但为了让它看起来像这样,我必须添加硬编码大小限制,从而使其 none 自适应。
这是在所有图像视图上使用 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>
如何使这个自适应并看起来仍然像第一张图片?
您可以使用 RelativeLayout.
而不是 ConstraintLayout<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="@drawable/first" />
<ImageView
android:id="@+id/sec"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/first"
android:layout_centerHorizontal="true"
android:src="@drawable/sec" />
<ImageView
android:id="@+id/third"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/sec"
android:layout_centerHorizontal="true"
android:src="@drawable/third" />
</RelativeLayout>
您可以使用 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>
然后您可以在任何其他布局中使用它。
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>
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>
我有办法完成这项工作。包括 android.support.constraint.Guideline
现在您可以将所有三个元素排列在屏幕中央,一个在另一个下方,如图所示。
由于我使用的图像很小,所以它看起来很小并且贴在屏幕顶部。但在您的情况下,这看起来符合预期。所有图像都使用 height
和 width
作为 wrap_content
对齐屏幕顶部和中心
下面是 xml 相同的
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/left_guideline"
app:layout_constraintGuide_percent=".02"
android:orientation="vertical"
/>
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/right_guideline"
app:layout_constraintGuide_percent=".98"
android:orientation="vertical"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/profile"
android:id="@+id/iconOne"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="@+id/left_guideline"
app:layout_constraintRight_toRightOf="@+id/right_guideline"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iconTwo"
android:src="@drawable/contactus"
app:layout_constraintTop_toBottomOf="@id/iconOne"
app:layout_constraintLeft_toLeftOf="@+id/left_guideline"
app:layout_constraintRight_toRightOf="@+id/right_guideline"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iconThree"
android:src="@drawable/pricelist"
app:layout_constraintTop_toBottomOf="@id/iconTwo"
app:layout_constraintLeft_toLeftOf="@+id/left_guideline"
app:layout_constraintRight_toRightOf="@+id/right_guideline"
/>
</android.support.constraint.ConstraintLayout>
将中心与屏幕对齐
现在如果你想让这三张图片居中显示,那么下面的方法就可以了。
对应的xml会是
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/left_guideline"
app:layout_constraintGuide_percent=".02"
android:orientation="vertical"
/>
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/right_guideline"
app:layout_constraintGuide_percent=".98"
android:orientation="vertical"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/profile"
android:id="@+id/iconOne"
app:layout_constraintBottom_toTopOf="@id/iconTwo"
app:layout_constraintLeft_toLeftOf="@+id/left_guideline"
app:layout_constraintRight_toRightOf="@+id/right_guideline"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iconTwo"
android:src="@drawable/contactus"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iconThree"
android:src="@drawable/pricelist"
app:layout_constraintTop_toBottomOf="@id/iconTwo"
app:layout_constraintLeft_toLeftOf="@+id/left_guideline"
app:layout_constraintRight_toRightOf="@+id/right_guideline"
/>
</android.support.constraint.ConstraintLayout>
这些示例将为我们提供 ConstraintLayout 的良好开端
您可以使用 Linearlayout
来解决这个问题,您可以为每张图片设置权重,使其正确对齐并适合所有屏幕尺寸
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:padding="10dp"
android:src="@drawable/top_image"
android:layout_weight="1"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:padding="10dp"
android:layout_marginTop="-100dp"
android:src="@drawable/middle_image"
android:layout_weight="1"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:padding="10dp"
android:layout_marginTop="-100dp"
android:src="@drawable/bottom_image"
android:layout_weight="1" />
</LinearLayout>
欢迎编辑
android:padding="10dp"
android:layout_marginTop="-100dp"
android:src="@drawable/bottom_image"
android:layout_weight="1"