今天学习了Android Studio的相对布局RelativeLayout
1. 相对布局(重点):相对布局是通过相对定位的方式让控件出现在布局任意位置;
在相对布局中如果不指定控件摆放的位置,那么控件都会被默认放在RelativeLayout的左上角。因此要先指定第一个控件的位置,再根据一个控件去给其他控件布局。
2. RelativeLayout常见属性:
相对于父元素给控件布局
android:layout_centerHrizontal 若为ture水平居中
android:layout_centerVertical 若为ture垂直居中
android:layout_centerInparent 相对于父元素完全居中
android:layout_alignParentBottom 位于父元素的下边缘
android:layout_alignParentLeft 位于父元素的左边缘
android:layout_alignParentRight 位于父元素的右边缘
android:layout_alignParentTop 位于父元素的上边缘
android:layout_alignWithParentIfMissing 如果对应的兄弟元素找不到的话就以父元素做参照物
属性值必须为id的引用名“@id/id-name”
android:layout_below 位于元素的下方
android:layout_above 位于元素的的上方
android:layout_toLeftOf 位于元素的左边
android:layout_toRightOf 位于元素的右边
android:layout_alignTop 该元素的上边缘和某元素的的上边缘对齐
android:layout_alignLeft 该元素的左边缘和某元素的的左边缘对齐
android:layout_alignBottom 该元素的下边缘和某元素的的下边缘对齐
android:layout_alignRight 该元素的右边缘和某元素的的右边缘对齐
给属性赋予像素值
android:layout_marginBottom 底边缘的距离
android:layout_marginLeft 左边缘的距离
android:layout_marginRight 右边缘的距离
android:layout_marginTop 上边缘的距离
利用今天学的这些知识,做了个小Demo;
首先利用百度图库找skype的图片;然后就是利用阿里图标找这些图标;然后下载个取色器;
上代码:
<?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="vertical" //对局为垂直对齐 android:background="#00aff0" //设置背景色,用取色器 tools:context=".MainActivity"> <RelativeLayout //采用相对布局 android:paddingLeft="30dp" android:paddingRight="30dp" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="0dp"> <Button android:textColor="@android:color/white" android:id="@+id/bt_ms_account" android:background="@drawable/bg_blue" android:layout_alignParentBottom="true" android:text="MS账号" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/bt_sk_account" android:textColor="@android:color/white" android:layout_marginBottom="20dp" android:layout_above="@id/bt_ms_account" android:background="@drawable/bg_blue" android:text="skype用户" android:layout_width="match_parent" android:layout_height="wrap_content" /> <ImageView android:id="@+id/im_count" android:layout_centerHorizontal="true" android:layout_marginBottom="0dp" android:layout_above="@id/bt_ms_account" android:src="@drawable/icon" android:layout_width="wrap_content" android:layout_height="match_parent" /> </RelativeLayout> <RelativeLayout android:layout_weight="1" android:layout_width="match_parent" android:layout_height="0dp"> <LinearLayout android:layout_marginTop="20dp" android:layout_marginLeft="80dp" android:layout_marginRight="80dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:layout_weight="1" android:src="@drawable/windows" android:layout_width="0dp" android:layout_height="22dp" /> <ImageView android:layout_weight="1" android:src="@drawable/mail" android:layout_width="0dp" android:layout_height="22dp" /> <ImageView android:layout_weight="1" android:src="@drawable/skype" android:layout_width="0dp" android:layout_height="22dp" /> <ImageView android:layout_weight="1" android:src="@drawable/xbox" android:layout_width="0dp" android:layout_height="22dp" /> <ImageView android:layout_weight="1" android:src="@drawable/cloud" android:layout_width="0dp" android:layout_height="22dp" /> </LinearLayout> <TextView android:id="@+id/tv_count" android:textColor="@android:color/white" android:background="#00a3de" android:padding="15dp" android:layout_alignParentBottom="true" android:textSize="20dp" android:text="创建账号" android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_login" android:textColor="@android:color/white" android:gravity="center" android:layout_marginBottom="25dp" android:layout_above="@id/tv_count" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登录"/> </RelativeLayout></LinearLayout>
已上就是今天的学习内容,加油!