build.gradle (app):
//Android Studio 4.0 或更高版本
android {
...
buildFeatures {
viewBinding true
}
}
activity_main:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="@+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
進行build 或 rebuild後,每個 Layout 就會對應生成一個綁定類別。如:MainActivity 的 Layout 為 activity_main,就會產生一個「ActivityMainBinding.java」
MainActivity:
public class MainActivity extends AppCompatActivity {
private ResultProfileBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ResultProfileBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
setContentView(view);
binding.tvText.setText("hello world");
}
}
謝謝大家願意花時間閱讀,小弟弟我在此鞠躬