简体   繁体   English

使用 ConstraintLayout 进行绝对定位

[英]Use ConstraintLayout for absolute positioning

I am trying to bring an older app back to life.我正试图让一个旧的应用程序起死回生。 I used a library to position locations of stores on a floor plan, but it seems deprecated.我使用了一个库来 position 平面图上的商店位置,但它似乎已被弃用。

My attempt with ConstraintLayout: I positioned all items in the editor and pushed the "Infer Constraints" button, as suggested somewhere on StackOverflow.我对 ConstraintLayout 的尝试:我在编辑器中放置了所有项目并按下了“推断约束”按钮,正如 StackOverflow 上某处所建议的那样。 It worked great... until I ran my app on my device, and noticed all poi's all over the place.它工作得很好......直到我在我的设备上运行我的应用程序,并注意到所有的 poi 到处都是。

There is a deprecated view which supports layout_marginLeftPercent etc, but I can't seem to find the ConstraintLayout alternative?有一个支持 layout_marginLeftPercent 等的已弃用视图,但我似乎找不到 ConstraintLayout 替代方案? Any suggestions?有什么建议么?

This can be accomplished with ConstraintLayout using Guidelines.这可以通过使用 Guidelines 的 ConstraintLayout 来完成。 So if you want a dot to be placed 24% from the top of the screen and 14% from the left, your code might look something like this:因此,如果您希望将一个点放置在距屏幕顶部 24% 的位置和距屏幕左侧 14% 的位置,您的代码可能如下所示:

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

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.14" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.24" />

    <ImageView
        android:id="@+id/dot"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/guideline2"
        app:layout_constraintEnd_toStartOf="@+id/guideline"
        app:layout_constraintStart_toStartOf="@+id/guideline"
        app:layout_constraintTop_toTopOf="@+id/guideline2"
        app:srcCompat="@android:color/background_dark" />
</android.support.constraint.ConstraintLayout>

More documentation and information about this can be found here可以在此处找到有关此的更多文档和信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM