【问题标题】:Android Constraint Layout not working / not responsiveAndroid 约束布局不工作/不响应
【发布时间】:2018-06-10 22:43:32
【问题描述】:

我正在尝试创建以下布局。我已经读过约束布局是我应该使我的布局响应。我尝试将我的约束布局包含在线性布局中,希望对齐可以修复但无济于事。 (不确定我的做法是否正确)

非常感谢任何帮助。

这是它在 android studio 中的样子:

这是我的 xml 代码:

<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_rightwashed"
    android:orientation="vertical">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <Button
            android:id="@+id/menu_btn"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_margin="5dp"
            android:background="@drawable/burger_btn" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:orientation="vertical">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">


            <ImageView
                android:id="@+id/calendar_btn"
                android:layout_width="190dp"
                android:layout_height="190dp"
                android:layout_alignParentBottom="true"
                android:layout_marginBottom="8dp"
                android:layout_marginEnd="16dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:background="@android:color/transparent"
                android:layoutDirection="inherit"
                android:src="@drawable/calendartrain"
                android:visibility="visible"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="1.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.48000002" />

            <ImageView

                android:id="@+id/skillstree_btn"
                android:layout_width="190dp"
                android:layout_height="190dp"
                android:layout_alignParentBottom="true"
                android:layout_marginBottom="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layoutDirection="inherit"
                android:src="@drawable/skillstreetrain"
                android:visibility="visible"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.04"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.58000004" />

            <ImageView
                android:id="@+id/completed_btn"
                android:layout_width="190dp"
                android:layout_height="190dp"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_marginBottom="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layoutDirection="inherit"
                android:src="@drawable/completedtrain"
                android:visibility="visible"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.22"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.24000001" />


        </android.support.constraint.ConstraintLayout>

    </LinearLayout>


    <RelativeLayout
        android:id="@+id/leftDrawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start">

        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/background_light" />

        <include
            layout="@layout/drawer"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentEnd="true"
            android:layout_alignParentTop="true" />


    </RelativeLayout>

</android.support.v4.widget.DrawerLayout>

【问题讨论】:

  • 为什么必须有三个不同的ImageViews?
  • @Jantzilla 每个按钮都应该打开一个不同的视图,这就是为什么我有 3 个。我为三个图像视图中的每一个添加了一个 onClickListener。
  • 您应该可以使用一个ImageView 并设置一个触摸监听器来检索坐标并根据触摸位置打开不同的视图。

标签: android android-layout android-studio android-constraintlayout


【解决方案1】:

您的布局取决于您在设计器中模拟的任何设备的尺寸。当屏幕尺寸或尺寸发生变化时,您的拼图排列方式会出现不匹配。

如果您想保留三个独立的图像,那么我建议您修改图像以沿水平轴和垂直轴组合在一起,使碎片的圆角适合封闭的ConstraintLayout 的角落。完成后,您可以旋转 ConstraintLayout 以显示您的倾斜拼图。

这是一个快速模型的图像。因为我没有你的图片,所以我使用了方块而不是拼图。不过概念是一样的。

这是 XML:

<android.support.constraint.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.constraint.ConstraintLayout
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:rotation="45"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">


        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@android:color/holo_green_light"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@android:color/holo_blue_light"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@android:color/holo_red_light"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@android:color/holo_orange_light"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />
    </android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-09
    • 2017-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多