Рано закапывать Java

image



«» Java, , — , . DSL Android !



Java .



float dp = getResources().getDisplayMetrics().density;

FrameLayout root = new FrameLayout(this);
root.setBackgroundColor(RED);

root.setLayoutParams(
        new ViewGroup.LayoutParams(
                MATCH_PARENT,
                (int)(100f*dp)));

FrameLayout child = new FrameLayout(this);
child.setBackgroundColor(GREEN);

FrameLayout.LayoutParams childParams =
        new FrameLayout.LayoutParams(
                (int)(50f*dp),
                (int)(50f*dp));

childParams.gravity = CENTER;

child.setLayoutParams(childParams);

root.addView(child);
      
      





:







, ( ). , . 3 , , , . , , , - ?



Java ? — xml Java.



xml



3.



— .

Android , inflate findViewById? , .



— .



<FrameLayout
    android:background="#f00"
    android:layout_width="match_parent"
    android:layout_height="100dp">

    <FrameLayout
        android:background="#0f0"
        android:layout_gravity="center"
        android:layout_width="50dp"
        android:layout_height="50dp"/>

</FrameLayout>
      
      







, «android:», .



— .

, 10dp.



<ImageView
    android:layout_width="@dimen/avatarSide"
    android:layout_height="@dimen/avatarSide"/>

<TextView
    android:layout_width="@dimen/avatarSide + 10dp"
    android:layout_height="wrap_content"/>
      
      







xml .



Anko?



Anko — DSL, Kotlin.



frameLayout {

    backgroundColor = RED

    frameLayout {
        backgroundColor = GREEN
    }.lparams(dip(50), dip(50)) {
        gravity = CENTER
    }

}.lparams(matchParent, dip(100))
      
      





, Java!



, , , . 500 — , — .



JAnko



, Java .



new frameLayout(this) {{

    new lparams(this) {{
        width  = MATCH_PARENT;
        height = dip(100);
    }}._();

    backgroundColor = RED;

    new frameLayout(this) {{

        new lparams(this) {{
            width  = dip(50);
            height = dip(50);
            gravity = CENTER;
        }}._();

        backgroundColor = GREEN;

    }}._();

}}._();
      
      





. -.



class A {
    // block
    {
        // some code
    }
}
      
      





Java , .



.



new imageView(this) {{

    new lparams(this) {{
        width = dimen(R.dimen.avatarSide);
        height = dimen(R.dimen.avatarSide);
    }}._();

}}._();

new textView(this) {{

    new lparams(this) {{
        width = dimen(R.dimen.avatarSide) + dip(10);
        height = WRAP_CONTENT;
    }}._();

}}._();
      
      





.







scala. proof of concept — .





0). Kotlin :



object : frameLayout(this) {
    init {

        object : lparams(this) {
            init {
                width = MATCH_PARENT
                height = dip(100f)
            }
        }.`_`()

        backgroundColor = RED

        object : frameLayout(this) {
            init {

                object : lparams(this) {
                    init {
                        width = dip(50f)
                        height = dip(50f)
                        gravity = CENTER
                    }
                }.`_`()

                backgroundColor = GREEN

            }
        }.`_`()

    }

}.`_`()
      
      





1) aar 12

2) Idea

3) Java Java









Anko, .



, - , Litho, measurment layout . eirnym .



All Articles