可调节的自定义UI

时间:2018-02-23 21:10:59

标签: android android-custom-view android-constraintlayout

我有一个由3个元素组成的自定义视图。 我希望视图适应它可用的大小。 我希望它看起来像this

但为了让它看起来像这样,我不得不添加硬编码大小限制因此使其无法自适应。

这是在所有图片视图上使用match_constraint的方式(不是我希望它看起来像this的方式):

这是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>

如何使这种自​​适应仍然看起来像第一张图像?

4 个答案:

答案 0 :(得分:6)

您可以使用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>

enter image description here

enter image description here

<强> 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>

enter image description here

答案 1 :(得分:1)

您可以使用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>

Images not aligned properly because I have not sized them perfectly.

答案 2 :(得分:1)

我有办法让这项工作成功。包括android.support.constraint.Guideline

现在,您可以将所有三个元素排列在屏幕中心,另一个排列在另一个下方,如图所示。

由于我使用的图像很小,它看起来很小并且附着在屏幕顶部。但在你的情况下,这将看起来像预期的那样。所有图片都使用heightwidth作为wrap_content

对齐屏幕顶部和中心

enter image description here

以下是相同

的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>

将中心与屏幕对齐

现在,如果你想让这三个图像成为屏幕的中心,那么下面的方法就会很好。

enter image description here

对应的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的良好开端

Reference OneReference TwoReference Three

答案 3 :(得分:1)

您可以使用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"
相关问题