《第二行代碼》 第一章 LinearLayout佈局

控件我就講了三個常用的TextView,EditText,ImageView

佈局和控件區別:準確說LinearLayout也是控件,因爲它裏面還能放其他控件,所以我們也可以叫它是佈局

單靠控件很難做出複雜的佈局,必須結合佈局纔可以

LinearLayout稱作線性佈局

1、android:orientation屬性設置控件是垂直排列還是橫向排列

android:orientation="vertical" 垂直排列
android:orientation="horizontal" 橫向排列(默認值)

2、Weight權重

它是設給子控件,用來設置所佔LinearLayout的比例

情況一:都設置0dp,這種情況比較簡單,就是按比例劃分
示例代碼:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".groupview.LinearLayoutActivity">

<TextView
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="#ff0000"
    android:text="One"
    android:textSize="30sp"
    android:gravity="center"/>

<TextView
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="#00ff00"
    android:text="Two"
    android:textSize="30sp"
    android:gravity="center"/>
</LinearLayout>

效果圖:

情況二:一個wrap_content,一個0dp
先給wrap_content,剩下的空間再按比例再給

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".groupview.LinearLayoutActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="#ff0000"
    android:text="One"
    android:textSize="30sp"
    android:gravity="center"/>

<TextView
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="#00ff00"
    android:text="Two"
    android:textSize="30sp"
    android:gravity="center"/>
</LinearLayout>  

效果圖:

本身One的wrap_content就需要空間,後面剩餘空間再按1:1平分

情況二:都是wrap_content
這種情況直接按比例劃分

情況三:都是match_parent
這種情況比較複雜,可能有些控件看不見了,顯示的比例也可能不是你設置的比例。可以直接放棄掉。記住0dp加設比例情況就好了

3、爲LinearLayout中的控件之間設置分割線

這個之前寫過一篇文章
https://www.jianshu.com/p/4e5ac4f09f20

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