zoukankan      html  css  js  c++  java
  • android第一行代码-1.项目结构

    0.项目结构

    一个简单的android项目结构大致如下

    入口就是MainActivity这个类,如果对于一个陌生的项目,最好的办法是看AndroidMainifest.xml,如下

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="activitytest.example.com.firstactivity">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name"
                android:theme="@style/AppTheme.NoActionBar">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>

    可以看到android:name是.MainActivity,从AndroidMainifest.xml可以看出该项目只有一个activity,活动的入口即是MainActivity。

    1.项目结构

    通过activity,就能很方便找到对应的项目结构了。

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.activity_main);
            final Button button = (Button) findViewById(R.id.button_1);
            final Button button_finish = (Button) findViewById(R.id.button_finish);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "you touch this button", Toast.LENGTH_LONG).show();
                }
            });
    
            button_finish.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });

    进入MainActivity,找到onCreate方法,这是activity最先调用的方法,setContentView(R.layout.activity_main)可以为活动加载一个布局,此处静态资源的引用的是activity_main.xml,注意R这个对象,它可以调用res文件夹以下的资源。

    activity_main.xml相当于mvc中的v,我们来看看它长啥样

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <Button
            android:id="@+id/button_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="alex"
            />
    
        <Button
            android:id="@+id/button_finish"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="finish"/>
    </LinearLayout>

    通过自己添加的组件,即可在activity中的布局中显示出来。

  • 相关阅读:
    计算机网络【七】:可靠传输的实现 (tcp窗口滑动以及拥塞控制)【转】
    计算机网络【六】:传输层-TCP概述 【转】
    计算机网络【五】:路由选择协议 【转】
    计算机网络【三】:数据链路层 【转】
    计算机网络【二】:物理层【转】
    计算机网络【一】:概述 【转】
    装饰模式-Decorator
    Java中的文件上传和下载
    模板方法模式-TemplateMethod
    策略模式-Strategy
  • 原文地址:https://www.cnblogs.com/alexkn/p/5448736.html
Copyright © 2011-2022 走看看