How to use Snackbar in Android Kotlin?
步骤1 ? 在Android Studio中创建一个新项目,转到文件? 新建项目,并填写所有必需的详细信息以创建一个新项目。
第2步 ? 将以下代码添加到res / layout / activity_main.xml。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | <?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" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="50dp" android:text="Tutorials Point" android:textAlignment="center" android:textColor="@android:color/holo_green_dark" android:textSize="32sp" android:textStyle="bold" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Click the button to display snackBar" android:textColor="@android:color/background_dark" android:textSize="24sp" android:textStyle="bold|italic" /> <Button android:id="@+id/btnSnackBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/textView" android:layout_centerInParent="true" android:layout_marginTop="10dp" android:text="Tap To Display SnackBar" android:textColor="@android:color/background_dark" /> </RelativeLayout> |
第3步? 将以下代码添加到src / MainActivity.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import android.graphics.Color import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.Button import android.widget.TextView import com.google.android.material.snackbar.Snackbar class MainActivity : AppCompatActivity() { lateinit var button: Button override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) title ="KotlinApp" button = findViewById(R.id.btnSnackBar) button.setOnClickListener { val snackBar = Snackbar.make( it,"Replace with your own action", Snackbar.LENGTH_LONG ).setAction("Action", null) snackBar.setActionTextColor(Color.BLUE) val snackBarView = snackBar.view snackBarView.setBackgroundColor(Color.CYAN) val textView = snackBarView.findViewById(com.google.android.material.R.id.snackbar_text) as TextView textView.setTextColor(Color.BLUE) snackBar.show() } } } |
第四步 ? 将以下代码添加到androidManifest.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.kotlipapp"> <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"> <intent-filter> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
"图标。选择您的移动设备作为选项,然后检查将显示默认屏幕的移动设备-
单击此处下载项目代码。