Kotlin / anko

Pleasant Android application development
Apache License 2.0
15.88k stars 1.29k forks source link

layout_toLeftOf, layout_toRightOf, layout_above and layout_below not supported #646

Open Hawkings opened 6 years ago

Hawkings commented 6 years ago

With RelativeLayout, it's sometimes necessary to use those properties, but they don't seem to be supported by Anko. For example, if I create an XML file with the following content:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <ImageView
        android:id = "@+id/my_image"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:layout_alignParentTop ="true" />
    <RelativeLayout
        android:id="@+id/layout_bottom"
        android:layout_width="fill_parent"
        android:layout_height = "50dp"
        android:layout_alignParentBottom = "true">
        <Button
            android:id = "@+id/but_left"
            android:layout_width = "80dp"
            android:layout_height = "wrap_content"
            android:text="&lt;"
            android:layout_alignParentLeft = "true"/>
        <TextView
            android:layout_width = "fill_parent"
            android:layout_height = "wrap_content"
            android:text="TEST"
            android:layout_toLeftOf = "@+id/but_right"
            android:layout_toRightOf = "@id/but_left" />
        <Button
            android:id = "@id/but_right"
            android:layout_width = "80dp"
            android:layout_height = "wrap_content"
            android:text="&gt;"
            android:layout_alignParentRight = "true"/>
    </RelativeLayout>
</RelativeLayout>

And then convert it to Anko DSL, this code is generated (excluding imports):

class SomeActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super<Activity>.onCreate(savedInstanceState)

        relativeLayout {
            imageView {
                id = Ids.my_image
            }.lparams(width = wrapContent, height = wrapContent) {
                alignParentTop()
            }
            relativeLayout {
                id = Ids.layout_bottom

                button("<") {
                    id = Ids.but_left
                }.lparams(width = dip(80), height = wrapContent) {
                    alignParentLeft()
                }
                textView("TEST").lparams(width = matchParent, height = wrapContent) {
                    toRightOf(R.id.but_left)
                    toLeftOf(Ids.but_right)
                }
                button(">") {
                    id = R.id.but_right
                }.lparams(width = dip(80), height = wrapContent) {
                    alignParentRight()
                }
            }.lparams(width = matchParent, height = dip(50)) {
                alignParentBottom()
            }
        }
    }

    private object Ids {
        val but_left = 1
        val layout_bottom = 2
        val my_image = 3
    }
}

Apart from the missing id but_right (which is an easily solvable issue), the problem is that toRightOf and toLeftOf don't exist, and that code does not compile.

I am using Kotlin 1.2.50 with Anko 0.10.5 in Android Studio 3.1.3.

senkwe commented 6 years ago

You should use rightOf() and leftOf() instead of toLeftOf or toRightOf()