Android - 线性布局与梅花布局
写在前面
新版Android Studio主题亮色的优先级要高于按钮XML的优先级(暗黑模式下尤为明显),所以在修改Button背景颜色时,无法修改也无法引入drawable样式的xml。
需要修改主题Style:把app/res/values目录下的Themes文件修改一下(你是哪个模式就修改哪个,不过目测估计白亮色模式不会有问题)
1 2
| <style name="Theme.项目名称" parent="Theme.MaterialComponents.DayNight.NoActionBar.Bridge">
|
线性嵌套布局
具体实现代码如下:两个线性布局嵌套外层垂直内部水平LinearLayout
version1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" android:orientation="vertical">
<LinearLayout android:layout_width="match_parent" android:layout_height="300dp" android:orientation="horizontal">
<Button android:layout_margin="5dp" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="#87CEFF" android:text="button1" android:textSize="15sp" android:textColor="@color/white"/>
<Button android:layout_margin="5dp" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="#EED2EE" android:text="button2" android:textSize="15sp" android:textColor="@color/white"/>
<Button android:layout_margin="5dp" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="#EEE9BF" android:text="button3" android:textSize="15sp" android:textColor="@color/white"/> </LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="400dp" android:layout_weight="1" android:orientation="vertical">
<Button android:layout_margin="5dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="#AEEEEE" android:text="button1" android:textSize="15sp" android:textColor="@color/white" />
<Button android:layout_margin="5dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="#66CDAA" android:text="button2" android:textSize="15sp" android:textColor="@color/white"/>
<Button android:layout_margin="5dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="#FF03DAC5" android:text="button3" android:textSize="15sp" android:textColor="@color/white" />
</LinearLayout> </LinearLayout>
|

梅花布局
images
具体代码实现如下 相对布局RelativeLayout
version1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent">
<ImageView android:id="@+id/btn_center" android:layout_width="100dp" android:layout_height="100dp" android:layout_centerInParent="true" android:src="@drawable/youtube"/>
<ImageView android:id="@+id/btn_top" android:layout_width="100dp" android:layout_height="100dp" android:layout_above="@+id/btn_center" android:layout_centerHorizontal="true" android:src="@drawable/android" />
<ImageView android:id="@+id/btn_bottom" android:layout_width="100dp" android:layout_height="100dp" android:layout_below="@+id/btn_center" android:layout_centerHorizontal="true" android:src="@drawable/apple" />
<ImageView android:id="@+id/btn_left" android:layout_width="100dp" android:layout_height="100dp" android:layout_centerVertical="true" android:layout_toLeftOf="@+id/btn_center" android:src="@drawable/ie" />
<ImageView android:id="@+id/btn_right" android:layout_width="100dp" android:layout_height="100dp" android:layout_centerVertical="true" android:layout_toRightOf="@+id/btn_center" android:src="@drawable/ie" /> </RelativeLayout>
|

图片可以自行插入,这里就不给出具体图片了相信各位都可以自行更换。
这里再给出一个Button版本的 对安卓比较熟悉的小伙伴可以把按钮设置圆角再添加图片到按钮上面去,这样效果会更好
version1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent">
<Button android:id="@+id/btn_center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Youtube" />
<Button android:id="@+id/btn_top" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/btn_center" android:layout_centerHorizontal="true" android:text="安卓" />
<Button android:id="@+id/btn_bottom" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/btn_center" android:layout_centerHorizontal="true" android:text="APPLE" />
<Button android:id="@+id/btn_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toLeftOf="@+id/btn_center" android:text="Github" />
<Button android:id="@+id/btn_right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@+id/btn_center" android:text="triangel" /> </RelativeLayout>
|

总结
安卓开发任重而道远,但十分方便,未来可以在嵌入式方面写一些安卓APP(如有需要),要不断学习,广泛涉猎。
:D 获取中...