Layout Xml로 구현한 Layout 속성 (width, height, weight, marginLeft, marginRight)이 있습니다.

<EditText android:id="@+id/saved"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/something"
    android:layout_marginLeft="10sp"
    android:layout_marginRight="10sp"
    android:layout_weight="1"
    android:background="@drawable/green"
    android:text="@string/initial_text"
    android:freezesText="true">
    <requestFocus />
</EditText>

위의 Layout Xml로 구현한 Layout 속성을 동적 코드로 만들면 아래와 같습니다.

EditText content = new EditText(getBaseContext());
MarginLayoutParams marginCotent = new ViewGroup.MarginLayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT );
marginCotent.setMargins( 1, 1, 0, 0 );
content.setLayoutParams( new LinearLayout.LayoutParams( marginCotent ) );
content.setBackgroundColor(Color.LTGRAY);
content.setGravity(Gravity.LEFT);
content.setFreezesText(true);
content.requestFocus();

마진 수치가 완전히 일치하진 않습니다. 유의하기 바랍니다.
눈여겨 볼게 하나 있는데 requestFocus 되어 있으면 해당 텍스트에디터에 자동 포커싱되게 됩니다.

Posted by 빈솔B
,