用weight平分屏幕

用weight平分屏幕:

 在LinearLayout中使用

android:orientation="horizontal"

或   //垂直自行腦補

android:orientation="vertical"

,控件寬用0dp,添加android:layout_weight="1"

如此操作,三個控件都用weight  1:1:1  控件可以居中,顯示效果更好,高度按需要給

weight  1:2:1什麼的可以靈活給,出來效果就不一樣

 

在RelativeLayout佈局中卻沒有提供weight的屬性

//RelativeLayout控件平分屏幕
<RelativeLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <View android:id="@+id/strut"
        android:layout_width="0dp"
        android:layout_height="0dp" 
        android:layout_centerHorizontal="true"/>
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_alignRight="@id/strut"
        android:layout_alignParentLeft="true"
        android:text="Left"/> 
    <Button 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/strut"
        android:layout_alignParentRight="true"
        android:text="Right"/>
</RelativeLayout>

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章