布局效果
本次演示的是数量加减框,即以下图片效果
自定义布局的好处
引入自定义布局和自定义控件的应用十分的广泛,在实际应用中被广泛使用。同时,自定义布局还能解决如果我们只是普通的在主布局中直接写一段布局的代码,无法在其他 activity 都能调用的问题,也就说,如果不使用自定义布局,我们必须在每一个布局中都写上同样的布局代码。从而可见,自定义布局的好处。
自定义布局的步骤
以下我将一数量加减框为例,一步一步的演示如何自定义布局
引入布局
在 layout 文件夹下创建一个布局源文件,名字为activity_add_and_decrease,代码如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
tools:context=".AddAndDecreaseButton">
<Button
android:id="@+id/btnDecrease"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text