Android開發自學筆記(四):APP布局下_Android

來源:互聯網
上載者:User

篇幅較長遂分成上下兩篇,上一篇我們已經快要一氣呵成了,但是美中不足的是,這個介面並不能討得美工MM的歡心,美工MM曾寄希望於您,卻交出這麼作出這麼一副死型樣,我都替你汗顏。

這個圖搜尋按鈕看起來馬馬虎虎,但是這個搜尋方塊真是有失我在美工MM心中的水準啊,這是因為我們把EditText和Button都的寬度都設定成按自身內容長度自適應,所以這一篇我們就來潤潤色,修一修這個布局。

Android在布局中引入了權重的概念,即如果給設定ViewGroup的總權重是,然後可以將權重分給它的子項目View各幾份,比如我們可以這段這個例子的總權重為5,然後將EditText的權重設定4,而Button的權重設定為0,這樣EditText就會實際利用這個LinearLayout的寬度的4/5,而Button只有1/5,我們在實際開發中並不能很好的定義一個View的具體寬度,所以我們可以藉助這種權重分成的方式可以很好的解決這個問題。

引入權重

layout_weight屬性即定義了權重,每一個View的預設權重為0,所以如果不顯示寫出來是0,但我這邊需要顯示的寫出Linearlayout的權重為5,EditText和Button則分別為4和1。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_weight="5" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <EditText android:id="@+id/edit_message"  android:layout_weight="4"  android:layout_width="0dip"  android:layout_height="wrap_content"  android:hint="@string/edit_message" />  <Button   android:layout_weight="1"   android:layout_width="0dip"   android:layout_height="wrap_content"   android:text="@string/btn_message" /></LinearLayout>


值得一提的是,我們在開發中可以不應該過多的使用wrap_content,因為系統並不知道這個值究竟是多少而去做更多的計算,所以我們這邊既然已經有了權重的概念,那我們就可以將EditText和Button的layout_width設定為0dip。

重新運行程式

重新運行程式,應該就可以得到我們預想的效果了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.