詳解Android中weight的使用方法_Android

來源:互聯網
上載者:User

android中對weight的學習可以說是必須的,如果UI布局僅僅使用dp與sp等等,會讓布局顯得極度不靈活,畢竟各個手機螢幕大小不同,更別說是還有ipad之類的了,所以也是同做本人近期做的一個小UI來分享一下weight的使用。

    

  

左邊是各個螢幕的顯示效果,右邊是1080*1920螢幕的具體顯示效果。可以看到,不管螢幕如何變化,使用weight的布局中總是填充滿螢幕的,至於美觀效果就不說了,直接上代碼。

小編使用的android studio,eclipse使用者直接複製肯定會有問題,AS使用者直接複製修改一下中間的圖片便可以用啦。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   xmlns:tools="http://schemas.android.com/tools"   android:layout_width="match_parent"   android:layout_height="match_parent"   android:orientation="vertical">    <RelativeLayout     android:layout_width="match_parent"     android:layout_height="0dp"     android:layout_weight="0.5"     android:background="#7EB345">      <Button       android:layout_width="wrap_content"       android:layout_height="match_parent"       android:background="@android:color/transparent"       android:drawableLeft="@drawable/left_menu"       android:paddingLeft="17dp" />      <TextView       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:layout_centerInParent="true"       android:text="某某科技大學"       android:textSize="25sp" />      <Button       android:layout_width="wrap_content"       android:layout_height="match_parent"       android:layout_alignParentRight="true"       android:background="@android:color/transparent"       android:text="登陸"       android:textColor="#fff"       android:textSize="20sp" />    </RelativeLayout>    <TextView     android:layout_width="match_parent"     android:layout_height="0dp"     android:layout_weight="1.5"     android:background="@drawable/school" />     <LinearLayout     android:layout_width="match_parent"     android:layout_height="0dp"     android:layout_weight="1"     android:orientation="horizontal">      <Button       android:layout_width="0dp"       android:layout_height="match_parent"       android:layout_weight="1"       android:background="@android:color/transparent"       android:drawableTop="@mipmap/ic_launcher"       android:paddingTop="18dp"       android:text="校園新聞" />      <Button       android:layout_width="0dp"       android:layout_height="match_parent"       android:layout_weight="1"       android:background="@android:color/transparent"       android:drawableTop="@mipmap/ic_launcher"       android:paddingTop="18dp"       android:text="學術公告" />      <Button       android:layout_width="0dp"       android:layout_height="match_parent"       android:layout_weight="1"       android:background="@android:color/transparent"       android:drawableTop="@mipmap/ic_launcher"       android:paddingTop="18dp"       android:text="成績查詢" />   </LinearLayout>    <LinearLayout     android:layout_width="match_parent"     android:layout_height="0dp"     android:layout_weight="1"     android:orientation="horizontal">      <Button       android:layout_width="0dp"       android:layout_height="match_parent"       android:layout_weight="1"       android:background="@android:color/transparent"       android:drawableTop="@mipmap/ic_launcher"       android:paddingTop="18dp"       android:text="課表資訊" />      <Button       android:layout_width="0dp"       android:layout_height="match_parent"       android:layout_weight="1"       android:background="@android:color/transparent"       android:drawableTop="@mipmap/ic_launcher"       android:paddingTop="18dp"       android:text="圖書借閱" />      <Button       android:layout_width="0dp"       android:layout_height="match_parent"       android:layout_weight="1"       android:background="@android:color/transparent"       android:drawableTop="@mipmap/ic_launcher"       android:paddingTop="18dp"       android:text="飯卡消費" />   </LinearLayout>    <LinearLayout     android:layout_width="match_parent"     android:layout_height="0dp"     android:layout_weight="1"     android:orientation="horizontal">      <Button       android:layout_width="0dp"       android:layout_height="match_parent"       android:layout_weight="1"       android:background="@android:color/transparent"       android:drawableTop="@mipmap/ic_launcher"       android:paddingTop="18dp"       android:text="校園地圖" />      <Button       android:layout_width="0dp"       android:layout_height="match_parent"       android:layout_weight="1"       android:background="@android:color/transparent"       android:drawableTop="@mipmap/ic_launcher"       android:paddingTop="18dp"       android:text="線上諮詢" />      <Button       android:id="@+id/neirongbuju"       android:layout_width="0dp"       android:layout_height="match_parent"       android:layout_weight="1"       android:background="@android:color/transparent"       android:drawableTop="@mipmap/ic_launcher"       android:paddingTop="18dp"       android:text="查看更多" />   </LinearLayout>    </LinearLayout> 

以上就是本文的全部內容,希望對大家的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.