美文网首页
第一行代码(3)

第一行代码(3)

作者: bluewind1230 | 来源:发表于2018-01-18 21:47 被阅读0次
    <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"
        tools:context="com.example.my_imagebutton.MainActivity"
        android:orientation="vertical">
    
        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:text="button1"/>
    </LinearLayout>
    

    这里添加了一个button元素,并在BUtton元素的内部增加了几个属性,android:id是给当前的元素定义一个唯一标识符,之后可以在代码中对这个元素进行操作;
    android:layout_width指定了当前元素的宽度,match_parent表示让当前元素和父元素一样宽;
    android:layout_height表示当前元素的高度,wrap_content表示当前元素的高度刚好能包含里面的内容即可
    android:text指定了元素中显示的文字内容
    可以通过preview来预览:


    image.png
    public class MainActivity extends AppCompatActivity {
    private  String  TAG = "MainActivity";
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Log.e(TAG,"onCreate");//打印错误信息
        }
    

    这里调用了setContentView()方法来给当前活动加载一个布局,在setContentView()这个方法中,我们一般都会传入一个布局文件的id,在项目中添加的任何资源都会在R文件中生成一个相应的资源id,因此刚才创建的layout.xml的id已经添加到R文件中了;在代码中调用R.layout.activity_main就会得到activity_main.xml布局的id,然后将这个值传入setContentView()方法即可;
    所有的活动都要在AndroidManifest.xml中进行注册才能生效,打开此文件:

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

    活动的注册声明要放在<application>标签内,通过 <activity >标签来对活动进行注册,在 <activity >标签中使用android:name来指定具体注册哪一个活动,com.example.bluelesson.my_activity00.MainActivity是.MainActivity的缩写,最外一层的<manifest>已经通过package属性指定了包的名字com.example.bluelesson.my_activity00,因此这里直接省略包名;
    只是注册活动程序还不能跑起来,还需要配置一个主活动,不然程序不知道先启动哪一个活动;
    .在 <activity >标签内部加入 <intent-filter>标签,并在这个标签里添加下面这两句即可:

                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
    

    使用android:label指定活动中标题栏内容,标题栏显示在活动最顶部,给主活动指定的label不仅会成为标题栏中的内容,还会成为启动器中应用程序显示的名称,

    例如:

    
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.bluelesson.my_activity00">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="This is the first activity"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity android:name=".Main2Activity"></activity>
        </application>
    
    </manifest>
    

    效果:


    image.png

    另外需要注意的是,如果应用程序中没有声明任何一个活动作为主活动,此程序仍然可以正常安装,只是无法在启动器中看到或者打开这个程序,这种程序一般作为第三方服务供其他应用在内部进行调用,

    相关文章

      网友评论

          本文标题:第一行代码(3)

          本文链接:https://www.haomeiwen.com/subject/eofgoxtx.html