以下内容是我在跟着B站视频学习时记录下来的一些要点。
要点记录
基础
- 所有的activities都要在AndroidManifest注册
- 软件名-app-src-main-java-com.xxxxx 所有activities的代码在这里
布局
常用布局管理器:LinearLayout、RelativeLayout
注意,以下代码都是在activity_main.xml内
LInearLayout
- 最常用属性:id、layout_width、layout_height(wrap_content 根据内容长度;match_parent 上一节控件的长度;z自定义时单位选择dp)、background、layout_margin(边距)(外部)、layout_padding(类边距)(内部)、orientation(线性布局的方向)、gravity(内部对齐方式)、weight(权重,即分配完剩下的空间将按照权重分配)
- 实操小例子:
注意1:View是大写的V!一开始怎么改view的颜色他都不变,原来写成了view…
注意2:gravity和layout_gravity不一样!
注意3:每一种控件都是成对出现的,注意前后呼应的格式,如<View … />
例子:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<!-- 基础配置,界面中的总布局,宽度高度设为适应屏幕 方向设为垂直(所以里面的控件将按垂直分布)-->
<LinearLayout
android:id="@+id/11_1"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#FF0033"
android:orientation="vertical"
android:padding="20dp"
android:layout_marginBottom="200dp">
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0066FF" />
</LinearLayout>
<!-- 第一个控件也设为了LinerLayout布局方式 设置了定长与定宽(dp为单位)
layout_marginBottom是下边距 所以这个控件与下一个控件有个距离
padding是类边距 所以这个控件内的View有一个边框
-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="horizontal"
android:background="#00FF33"
android:layout_marginTop="10dp"
android:gravity="center">
<!-- 第二个控件,gravity是内部对齐方式 -->
<View
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#871F78"
android:layout_weight="1"/>
<View
android:layout_width="0dp"
android:layout_height="50dp"
android:background="#CD7F32"
android:layout_weight="1"/>
<View
android:layout_width="0dp"
android:layout_height="50dp"
android:background="#3399ff"
android:layout_weight="1"/>
<!--三个差不多的-->
</LinearLayout>
</LinearLayout>
效果:
RelativeLayout
1.特有属性:
layout_toLeftOf(在…左边)、layout_toRightOf、layout_alignBottom(与…底部对齐)、layout_alignParentBottom(与父控件底部对齐)、layout_below(在…下面)
2.实例:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 主布局-->
<View
android:id="@+id/view_1"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#000000"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>
<!--第一个控件A,放在了父控件的底部以及右边,也就是右下角 -->
<View
android:id="@+id/view_2"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#0066FF"
android:layout_above="@id/view_1"
android:layout_toLeftOf="@id/view_1"/>
<!--第二个控件B,放在了第一个控件的左边以及上边,也就是左上角 -->
<LinearLayout
android:id="@+id/LinearLayout_1"
android:orientation="horizontal"
android:padding="30dp"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_toLeftOf="@id/view_2"
android:background="#22EEFF">
<!--第三个控件C放在了第二个控件左边,设置了类边距 -->
<View
android:layout_width="70dp"
android:layout_height="match_parent"
android:background="#FF1122"/>
<!--第三个控件的第一个子控件C_1 红色的 -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:paddingLeft="10dp"
android:paddingBottom="20dp">
<!--第二个子控件C_2黑色的 并设置了类边距 -->
<View
android:id="@+id/view_3"
android:layout_width="50dp"
android:layout_height="match_parent"
android:background="#FF9900"/>
<!--第二个子控件的第一个子控件C_2_1 橙色 -->
<View
android:id="@+id/view_4"
android:layout_width="50dp"
android:layout_height="match_parent"
android:background="#AA4400"
android:layout_toRightOf="@+id/view_3"
android:layout_marginLeft="10dp"/>
<!--第二个子控件的第二个子控件c-2_2 棕色 设为在C_2_1的右边且有间距 -->
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
效果: