03. Android Studio creates a basic Activity

Create a basic Activity

 

 Open Android Studio and click file--->New Project, select No Activity to create an empty Activity

 

Click nex, and after the project is created, right-click New->Activity->Empty Activity in com.example.listviewtest

 Check Generate a Layout File to let Android Studio automatically create a layout layout xml file for us

 At this time, go to AndroidManifest.xml to register the newly created Activity (generally AndroidStudio will automatically register), if this is the first Activity in this empty project (Android: name is to specify which Activity to register), but now the project is still Can't start, because the main Activity has not been configured yet, so add the <intent-filter> tag to the tag <activity> and add <action> and <category> to this tag

 

       <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"></action>
                <category android:name="android.intent.category.LAUNCHER"></category>
            </intent-filter>
        </activity>

Add a kotlion plugin to build.gradle in the app directory

id 'kotlin-android-extensions'

 Now that the Activity has been created, let's try it in the created Activity

First, add a Button element in the .xml file of the Activity in the layout folder

<?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/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="BUTTON1"
        />

</LinearLayout>

Then add several attributes inside the Button element, android:id is to define a unique identifier for the current element, and then you can operate on this element in the code, android:layout_width specifies the width of the current element, here use match_parent to express The current element is as wide as the parent element, android:layout_height specifies the height of the current element. Here, wrap_content is used to indicate that the height of the current element can just contain the content inside. Android:text specifies the content displayed in the element

Then go to the Activity to load the layout

class FirstActivity : BaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.d("FirstActivity", "Task id is $taskId")
        setContentView(R.layout.XXXX_layout)
      
        
}

Finally run it

Guess you like

Origin blog.csdn.net/weixin_46511995/article/details/126047752