昨夜倒腾到1点,终于装好了Android Studio 。 却在更新上除了大问题。。。总是失败。。
作为一个小白,各种搜索无果。大汗。。
废话不多说了,还是进入正题!
总结一下目前的对于Android的认知:
终于知道了所谓的四大组件:活动、服务、广播接收器和内容提供器。(不过说实话,到目前为止,对这些东西还是很陌生)
手动创建活动的步骤:
1.在一个空项目中的main文件内的java包内,创建一个空的活动。自动生成如下代码
package com.example.dell.chapter2_activitytest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class FirstActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
//调用父类的onCreate()方法
super.onCreate(savedInstanceState);
}
}
2.现在开始创建布局以及加载布局
在app/res下创建一个layout的文件夹用以存放布局文件。然后右击添加一个Layout Source File(这里命名为first_layout),这里会生成一个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>
3.在该文件内添加一个按钮
<?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">
<Button
android:id="@+id/button_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 1"
/>
</LinearLayout>
在Button标签中,添加了Button的id。并且设置了按钮的宽和高。以及按钮的文本
4.将布局加载到活动中,修改后的代码如下:
package com.example.dell.chapter2_activitytest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class FirstActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
//调用父类的onCreate()方法
super.onCreate(savedInstanceState);
this.setContentView(R.layout.first_layout);//注意格式:res文件下的layout下的first_layout.xml布局文件
}
}
5.到此还没有结束,记得想AndroidMainifest文件中注册活动,并将活动设置为主活动:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dell.chapter2_activitytest">
<!--- 活动的注册要放到<application中>-->
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".FirstActivity"
android:label="this is FirstActivity">
<!--- 设置该活动为主程序 -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>>
</activity>
</application>
</manifest>
总结一下流程:活动的创建---->布局的构造----->布局加载到活动中----->活动注册(注意一定不能少了主活动的声明)
之前都没有接触过XML,看来是时候了!