文章目录
一、基本概念
开发商
Android是Google开发的操作系统。
开发工具
Eclipse(已废弃)
Android Studio
二、开始开发
AS连接mumu模拟器
首先下载一个mumu模拟器
进入bin目录下,打开cmd命令窗口,输入指令:
adb_server.exe connect 127.0.0.1:7555
AS已经可以看到mumu了:
成功在模拟器上运行:
第一个小程序
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
只有一个onCreate方法
布局管理器
相对布局(RelativeLayout)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<RelativeLayout
android:id="@+id/id01"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#000000" />
<RelativeLayout
android:id="@+id/id02"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_toRightOf="@id/id01"
android:background="#ff0033"></RelativeLayout>
<RelativeLayout
android:id="@+id/id03"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="@id/id02"
android:background="#0066ff"
android:padding="50dp">
<LinearLayout
android:layout_width="200dp"
android:layout_height="50dp"
android:background="#ff0033"
/>
</RelativeLayout>
</RelativeLayout>
与线性布局相比,多了一类标签。比如:android:layout_below=“@id/id02”
代表该标签的组件在目标组件的下面
线性布局(LinearLayout)
一个线性布局的demo
activity_main.xml
<?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"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#000000"
android:orientation="horizontal"
android:paddingLeft="20dp"
android:paddingTop="20dp"
android:paddingRight="20dp"
android:paddingBottom="20dp">
<view
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0033"></view>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="10dp"
android:background="#0066FF"
android:gravity="center"
android:orientation="horizontal"
android:paddingLeft="20dp"
android:paddingTop="20dp"
android:paddingRight="20dp"
android:paddingBottom="20dp">
<view
android:layout_width="0dp"
android:layout_height="60dp"
android:background="#ffffff"
android:orientation="horizontal"
android:layout_weight="1"></view>
<view
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@color/cardview_dark_background"
android:layout_weight="1"></view>
</LinearLayout>
</LinearLayout>
标签元素的含义都比较 见名知意
特别说明的是 android:layout_weight=“1” 这个元素 此处赋值1,代表的是比例,在同一个LinearLayout标签下的view,比如两个view块的layout_weight都是1,则代表两个view块的面积是1:1 其实这个数字就是这个标签在父标签页面上所占的比例(与其他标签相比)
TextView
实现
第一个页面跳转到第二个页面
第二个页面有text文本+icon
MainActivity
public class MainActivity extends AppCompatActivity {
private Button buttonText01;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 把布局文件里的btn与此处的button绑定
buttonText01 = findViewById(R.id.btn_text01);
buttonText01.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 跳转到text_view
Intent intent = new Intent(MainActivity.this, ViewActivity.class);
startActivity(intent);
}
});
}
}
buttonText01.setOnClickListener(new View.OnClickListener() {} 一个按钮事件
ViewActivity
略
自动初始化的
activity_main.xml
<?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"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_text01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:text="TextView"
android:layout_marginTop="200dp">
</Button>
</LinearLayout>
主要是一个按钮button
activity_view.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ViewActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我爱吃炸酱面"
android:textColor="#000000"
android:textSize="30sp"
android:layout_marginTop="30dp"
android:drawableRight="@drawable/customer">
</TextView>
<TextView
android:layout_width="400px"
android:layout_height="wrap_content"
android:text="我超级爱吃炸酱面"
android:textColor="#000000"
android:textSize="30sp"
android:ellipsize="end"
android:maxLines="1"
android:layout_marginTop="20dp">
</TextView>
</LinearLayout>
android:ellipsize=“end” 代表超过文本框边界的文字用…代替
android:drawableRight=“@drawable/customer” 从drawable文件夹中读取icon
android:textSize=“30sp” 字体大小
android:maxLines=“1” 最大行数