前言
LayoutCreator插件可以快速幫助生成xml布局中的代碼,但是有以下自動生成規則:
- 自動遍歷目標布局中所有帶id的文件, 無id的不會識別處理
- 控件生成的變量名默認為id名稱, 可以在彈出確認框右側的名稱輸入欄中自行修改
- 所有的Button或者帶clickable=true的控件, 都會自動在代碼中生成setOnClickListener相關代碼
- 所有EditText控件, 都會在代碼中生成非空判斷代碼, 如果為空會提示EditText的hint內容, 如果hint為空則提示xxx字符串不能為空字樣, 最后會把所有輸入框的驗證合並到一個submit方法中
- 會自動識別布局中的include標簽, 並讀取對應布局中的控件
使用效果
需要生成的xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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" tools:context=".MyLayoutDemoActivity"> <Button android:id="@+id/button1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="1" app:layout_constraintBottom_toTopOf="@+id/textView" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textView" android:layout_width="0dp" android:layout_height="wrap_content" android:text="content" android:gravity="center" app:layout_constraintTop_toBottomOf="@id/button1" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
自動生成的代碼
如何添加?
第一步
在Android studio 的菜單欄找到 File > Settings 點擊
第二步
找到Plugins , 在搜索欄輸入LayoutCreator 回車搜索,然后在點擊安裝 LayoutCreator,安裝完成后需要重啟Android studio
如何使用?
方式一
1.將鼠標點擊到指定Activity的Java文件的R.layout.xxxxx的布局代碼上
2.在Android studio 菜單欄點擊 code 找到 LayoutCreator 點擊,就可以進入創建頁面了,選擇你需要的命名與需要生成的View在點擊 Confirm
方式二
1.將鼠標點擊到指定Activity的Java文件的R.layout.xxxxx的布局代碼上
2.直接A/t + Insert 按鍵,找到LayoutCreator 點擊
end