線性佈局:案例
阿新 • • 發佈:2018-11-07
1.線性佈局:按鈕
主佈局資原始檔activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="385dp" android:layout_height="103dp" android:background="#ff00ff" android:gravity="center" android:orientation="horizontal" android:weightSum="1"> <LinearLayout android:layout_width="wrap_content" android:layout_height="64dp" android:layout_weight="0.05" android:background="@mipmap/ic_launcher"> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="64dp" android:layout_weight="0.05" android:background="@mipmap/ic_launcher_round"> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="64dp" android:layout_weight="0.05" android:background="@mipmap/x"> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="64dp" android:background="#39ff96" android:gravity="center|bottom" android:weightSum="1"> <Button android:id="@+id/button3" android:layout_width="69dp" android:layout_height="34dp" android:background="#00ff2a" android:text="按鈕1" /> <Button android:layout_width="10dp" android:layout_height="34dp" android:background="#39ff96"/> <Button android:layout_width="74dp" android:layout_height="36dp" android:background="#00ff2a" android:text="按鈕2" /> <Button android:layout_width="10dp" android:layout_height="34dp" android:background="#39ff96"/> <Button android:layout_width="69dp" android:layout_height="34dp" android:background="#00ff2a" android:text="按鈕3" /> <Button android:layout_width="10dp" android:layout_height="34dp" android:background="#39ff96"/> <Button android:layout_width="69dp" android:layout_height="34dp" android:background="#00ff2a" android:text="按鈕4" /> </LinearLayout> <LinearLayout android:layout_width="385dp" android:layout_height="141dp" android:background="#39ff96" android:gravity="bottom" android:weightSum="1"> <LinearLayout android:layout_width="match_parent" android:layout_height="93dp" android:layout_margin="20dp" android:background="#ffff00" android:layout_weight="0.28"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/xxxx" android:textSize="26dp" android:layout_weight="17" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:background="#cf570c" android:gravity="center" android:weightSum="1"> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginRight="20dp" android:background="#30f8ff" android:gravity="right|center" android:orientation="vertical"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#aff0f0" android:text="按鈕1" /> <Button android:layout_width="match_parent" android:layout_height="10dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#aff0f0" android:text="按鈕2" /> <Button android:layout_width="match_parent" android:layout_height="10dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#aff0f0" android:text="按鈕3" /> </LinearLayout> <Button android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕1" /> <Button android:id="@+id/button6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕2" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginLeft="20dp" android:background="#ff3078" android:gravity="center|left" android:orientation="vertical"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#fe97bb" android:text="按鈕1" /> <Button android:layout_width="match_parent" android:layout_height="10dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#fe97bb" android:text="按鈕2" /> <Button android:layout_width="match_parent" android:layout_height="10dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#fe97bb" android:text="按鈕3" /> </LinearLayout> </LinearLayout> </LinearLayout>
2.線性佈局:水果
主佈局資原始檔activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@mipmap/sgbj" android:gravity="center|bottom" android:weightSum="1"> <LinearLayout android:id="@+id/tv_sg" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center|top" android:layout_weight="1.04"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="歡迎朋友來品嚐" android:textSize="30sp" android:textColor="#ff0000" android:onClick="doStart"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="75dp" android:orientation="horizontal" android:weightSum="1"> <TextView android:layout_width="10dp" android:layout_height="match_parent" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:background="@mipmap/juzi" /> <TextView android:layout_width="20dp" android:layout_height="match_parent" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="0.32" android:background="@mipmap/lizi" /> <TextView android:layout_width="20dp" android:layout_height="match_parent" /> <TextView android:layout_width="74dp" android:layout_height="match_parent" android:background="@mipmap/caomei" /> <TextView android:layout_width="20dp" android:layout_height="match_parent" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="0.47" android:background="@mipmap/mangguo" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="25dp" android:orientation="horizontal"> <TextView android:layout_width="35dp" android:layout_height="match_parent" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="橘子" android:textSize="20dp" /> <TextView android:layout_width="55dp" android:layout_height="match_parent" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="梨子" android:textSize="20dp" /> <TextView android:layout_width="55dp" android:layout_height="match_parent" /> <TextView android:layout_width="74dp" android:layout_height="match_parent" android:text="草莓" android:textSize="20dp" /> <TextView android:layout_width="20dp" android:layout_height="match_parent" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="芒果" android:textSize="20dp"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="75dp" android:orientation="horizontal" android:weightSum="1"> <TextView android:layout_width="10dp" android:layout_height="match_parent" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:background="@mipmap/pingguo" android:layout_weight="0.53" /> <TextView android:layout_width="20dp" android:layout_height="match_parent" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="0.30" android:background="@mipmap/yezi" /> <TextView android:layout_width="20dp" android:layout_height="match_parent" /> <TextView android:layout_width="74dp" android:layout_height="match_parent" android:background="@mipmap/mugua" /> <TextView android:layout_width="20dp" android:layout_height="match_parent" /> <TextView android:layout_width="74dp" android:layout_height="match_parent" android:background="@mipmap/mihoutao" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="25dp" android:orientation="horizontal"> <TextView android:layout_width="35dp" android:layout_height="match_parent" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="蘋果" android:textSize="20dp" /> <TextView android:layout_width="55dp" android:layout_height="match_parent" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="椰子" android:textSize="20dp" /> <TextView android:layout_width="55dp" android:layout_height="match_parent" /> <TextView android:layout_width="74dp" android:layout_height="match_parent" android:text="木瓜" android:textSize="20dp" /> <TextView android:layout_width="10dp" android:layout_height="match_parent" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="獼猴桃" android:textSize="20dp"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="75dp" android:orientation="horizontal" android:weightSum="1"> <TextView android:layout_width="10dp" android:layout_height="match_parent" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:background="@mipmap/juzi" /> <TextView android:layout_width="20dp" android:layout_height="match_parent" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="0.32" android:background="@mipmap/lizi" /> <TextView android:layout_width="20dp" android:layout_height="match_parent" /> <TextView android:layout_width="74dp" android:layout_height="match_parent" android:background="@mipmap/caomei" /> <TextView android:layout_width="20dp" android:layout_height="match_parent" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:background="@mipmap/mangguo" android:layout_weight="0.47" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="25dp" android:orientation="horizontal"> <TextView android:layout_width="35dp" android:layout_height="match_parent" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="橘子" android:textSize="20dp" /> <TextView android:layout_width="55dp" android:layout_height="match_parent" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="梨子" android:textSize="20dp" /> <TextView android:layout_width="55dp" android:layout_height="match_parent" /> <TextView android:layout_width="74dp" android:layout_height="match_parent" android:text="草莓" android:textSize="20dp" /> <TextView android:layout_width="20dp" android:layout_height="match_parent" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="芒果" android:textSize="20dp"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="75dp" android:orientation="horizontal" android:weightSum="1"> <TextView android:layout_width="10dp" android:layout_height="match_parent" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:background="@mipmap/pingguo" android:layout_weight="0.53" /> <TextView android:layout_width="20dp" android:layout_height="match_parent" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="0.30" android:background="@mipmap/yezi" /> <TextView android:layout_width="20dp" android:layout_height="match_parent" /> <TextView android:layout_width="74dp" android:layout_height="match_parent" android:background="@mipmap/mugua" /> <TextView android:layout_width="20dp" android:layout_height="match_parent" /> <TextView android:layout_width="74dp" android:layout_height="match_parent" android:background="@mipmap/mihoutao" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="25dp" android:orientation="horizontal"> <TextView android:layout_width="35dp" android:layout_height="match_parent" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="蘋果" android:textSize="20dp" /> <TextView android:layout_width="55dp" android:layout_height="match_parent" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="椰子" android:textSize="20dp" /> <TextView android:layout_width="55dp" android:layout_height="match_parent" /> <TextView android:layout_width="74dp" android:layout_height="match_parent" android:text="木瓜" android:textSize="20dp" /> <TextView android:layout_width="10dp" android:layout_height="match_parent" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="獼猴桃" android:textSize="20dp"/> </LinearLayout> </LinearLayout>
3.線性佈局:藍墨雲
主佈局資原始檔activity_main.xml
<?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" android:orientation="vertical" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.myapplication04.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="80dp" android:orientation="horizontal" android:gravity="center_vertical" android:padding="10dp" android:background="#FFFFFF"> <TextView android:id="@+id/tv_toolimg" android:layout_width="60dp" android:layout_height="60dp" android:background="@mipmap/x" /> <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="張林波" android:textSize="20sp" android:layout_marginLeft="10dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=">" android:textSize="30sp" android:layout_marginLeft="180dp"/> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1sp" android:background="#00CD00"/> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp" android:orientation="horizontal" android:background="#FFFFFF" > <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3563" android:textColor="@color/colorPrimary"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="經驗值"/> </LinearLayout> <View android:layout_width="1sp" android:layout_height="match_parent" android:background="#00CD00"/> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1489" android:textColor="@color/colorPrimary"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="魅力值"/> </LinearLayout> <View android:layout_width="1sp" android:layout_height="match_parent" android:background="#00CD00"/> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="342" android:textColor="@color/colorPrimary"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="藍豆"/> </LinearLayout> <View android:layout_width="1sp" android:layout_height="match_parent" android:background="#00CD00"/> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="341" android:textColor="@color/colorPrimary"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="鮮花"/> </LinearLayout> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1sp" android:background="#00CD00"/> <View android:layout_width="match_parent" android:layout_height="20dp" /> <View android:layout_width="match_parent" android:layout_height="1sp" android:background="#00CD00"/> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal" android:gravity="center_vertical" android:padding="10dp" android:background="#FFFFFF"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我的空間"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=">" android:textSize="20sp" android:layout_marginLeft="250dp"/> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1sp" android:background="#00CD00"/> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal" android:gravity="center_vertical" android:padding="10dp" android:background="#FFFFFF"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我的收藏"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=">" android:textSize="20sp" android:layout_marginLeft="250dp"/> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1sp" android:background="#00CD00"/> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal" android:gravity="center_vertical" android:padding="10dp" android:background="#FFFFFF"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="收到的心意、卡片"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=">" android:textSize="20sp" android:layout_marginLeft="193dp"/> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1sp" android:background="#00CD00"/> <View android:layout_width="match_parent" android:layout_height="20dp" /> <View android:layout_width="match_parent" android:layout_height="1sp" android:background="#00CD00"/> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal" android:gravity="center_vertical" android:padding="10dp" android:background="#FFFFFF"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="分享給朋友"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="累計分享成功25次" android:layout_marginLeft="100dp" android:textColor="#C1CDC1"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=">" android:textSize="20sp" android:layout_marginLeft="20dp"/> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1sp" android:background="#00CD00"/> <View android:layout_width="match_parent" android:layout_height="20dp" /> <View android:layout_width="match_parent" android:layout_height="1sp" android:background="#00CD00"/> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal" android:gravity="center_vertical" android:padding="10dp" android:background="#FFFFFF"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="設定"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=">" android:textSize="20sp" android:layout_marginLeft="275dp"/> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1sp" android:background="#00CD00"/> <View android:layout_width="match_parent" android:layout_height="20dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center" android:background="#FFFFFF"> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:orientation="vertical" android:layout_weight="1" android:gravity="center"> <TextView android:layout_width="50dp" android:layout_height="50dp" android:background="#eeeeee" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:text="班課" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:orientation="vertical" android:layout_weight="1" android:gravity="center"> <TextView android:layout_width="50dp" android:layout_height="50dp" android:background="#eeeeee" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:text="發現" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:orientation="vertical" android:layout_weight="1" android:gravity="center"> <TextView android:layout_width="50dp" android:layout_height="50dp" android:background="#eeeeee" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:text="我的" /> </LinearLayout> </LinearLayout> </LinearLayout>
主介面類MainActivity