pocorall / scaloid

Scaloid makes your Android code easy to understand and maintain.
Other
2.09k stars 163 forks source link

XML-less layout with custom views. #55

Closed i-am-the-slime closed 10 years ago

i-am-the-slime commented 11 years ago

I have made two custom Views. How do I make them compatible with the XML-less approach?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/lockscreen_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
    <ViewStub
        android:id="@+id/lockscreen_stub"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
    />
    <com.menthal.nyx.view.LockScreenClock
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center|center_vertical|top"
            android:textSize="110dip"
            android:id="@+id/clock" android:textIsSelectable="false" android:textColor="@color/foreground"/>
    <com.menthal.nyx.view.LockScreenCircle
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/progress" android:layout_gravity="center_horizontal|bottom"
            android:layout_alignParentEnd="false"
            android:layout_alignParentBottom="true"
            android:layout_marginLeft="60dip"
            android:layout_marginBottom="20dip"
            android:layout_marginRight="60dip"/>
</RelativeLayout>

Is translated to this:

  override def onCreate(savedInstanceState: Bundle) {
    super.onCreate(savedInstanceState)

    contentView = new SRelativeLayout {
      SViewStub().<<.fill.>>
      Scom.menthal.nyx.view.LockScreenClock().textSize(110 dip)
      Scom.menthal.nyx.view.LockScreenCircle().<<.marginBottom(20 dip).marginLeft(60 dip).marginRight(60 dip).>>
    }
  }

Which is obviously wrong. How do I make S* versions of my custom views?

pocorall commented 11 years ago

You can make S... version of your custom view as shown below:

class SLockScreenClock extends LockScreenClock with TraitView[SLockScreenClock]

Actual trait that you extend is depends on what your base class inherits. If your base class inherits ViewGroup, you'd better to extend TraitViewGroup[SLockScreenClock].