
上QQ阅读APP看书,第一时间看更新
There's more...
In contrast to what we saw earlier, here is an example using LinearLayout just to center TextView (creating the same effect as the layout_center parameter of RelativeLayout):
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" > <TextView android:id="@+id/imageButton_speak" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Centered" /> </LinearLayout> </LinearLayout>
Notice this layout is one level deeper than the equivalent RelativeLayout (which is LinearLayout nested within the parent LinearLayout.) Though a simple example, it's a good idea to avoid unnecessary nesting as it can impact performance, especially when a layout is being repeatedly inflated (such as ListItem).