Basic use of Button in Android
1. Text size and color
<?xml version="1.0" encoding="utf-8"?>
<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>
The background color to take effect: changes
themes.xml
File night to see the situation changes.
Theme.MaterialComponents.DayNight.DarkActionBar.Bridge
2. Customize the background shape
The content of the file is:
<?xml version="1.0" encoding="utf-8"?>
<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"/>
Stroke
<?xml version="1.0" encoding="utf-8"?>
<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>
3. Customize the pressing effect
Create a drawable file
<?xml version="1.0" encoding="utf-8"?>
<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"/>
4. Click event
Add an attribute, showToast is the method name.
android:onClick="showToast"
Create a function in the corresponding Activity.
public void showToast(View view) {
// 提示信息 Toast.LENGTH_SHORT 短时间, Toast.LENGTH_LONG 长时间
Toast.makeText(this, "一个点击事件", Toast.LENGTH_SHORT).show();
}
The click event is not a Button but can also be set.