<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"
android:padding="15dp"
android:orientation="vertical"
tools:context=".ButtonActivity">
<Button
android:id="@+id/btn_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button"
android:textSize="25sp"
android:textColor="@color/teal_200"
android:background="@color/black"/>
LinearLayout>
使背景颜色生效: 改动
themes.xml
文件. 夜间的看情况改动.
Theme.MaterialComponents.DayNight.DarkActionBar.Bridge
文件内容为:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/orange_low" />
<corners android:radius="15dp" />
shape>
<Button
android:id="@+id/btn_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button"
android:textSize="25sp"
android:textColor="@color/red"
android:layout_margin="15dp"
android:background="@drawable/bg_btn2"/>
描边
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1dp"
android:color="@color/red" />
<corners android:radius="15dp" />
shape>
建立一个 drawable 文件
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<corners android:radius="15dp" />
<solid android:color="@color/yellow_blew" />
shape>
item>
<item android:state_pressed="false">
<shape android:shape="rectangle">
<corners android:radius="10dp" />
<solid android:color="@color/blue" />
shape>
item>
selector>
<Button
android:id="@+id/btn_4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button"
android:textSize="25sp"
android:textColor="@color/white"
android:layout_margin="15dp"
android:background="@drawable/bg_btn4"/>
添加一个属性, showToast 为方法名.
android:onClick="showToast"
在对应的 Activity 建立一个函数.
public void showToast(View view) {
// 提示信息 Toast.LENGTH_SHORT 短时间, Toast.LENGTH_LONG 长时间
Toast.makeText(this, "一个点击事件", Toast.LENGTH_SHORT).show();
}
点击事件不是 Button 也可以设置.