added support for android:layout_gravity, some refactoring