Androidgravity vs androidlayoutgravity

Note that Android defines two similar gravity attributes: android:gravity and android: layout_gravity. Here's the difference: android:gravity is a setting used by the view, whereas android:layout_gravity is used by the container (android.view.ViewGroup). For example, you can set android:gravity to center to have the text in the EditText centered within the control. Similarly, you can align an EditText to the far right of a LinearLayout (the container) by setting android:layout_gravity="right". See Figure 4-12 and Listing 4-27.

loq 0 sudĀ« 2:42 am i

Common Controls

Figure 4-12. Applying gravity settings

Figure 4-12. Applying gravity settings

Listing 4-27. Understanding the Difference Between android:gravity and android:layout_gravity

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">

<EditText android:layout_width="wrap_content" android:gravity="center" android:layout_height="wrap_content" android:text="one" android:layout_gravity="right"/> </LinearLayout>

As shown in Figure 4-12, the text is centered within the EditText and the EditText itself is aligned to the right of the LinearLayout.

0 0

Post a comment