activityBinding = MainActivityBinding.inflate(getLayoutInflater());
setContentView(activityBinding.getRoot());
activityBinding.container.setBackgroundColor(Color.DKGRAY);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, MainFragment.newInstance())
.commitNow();
}
}
}
Fragment中使用
package com.viewbinding.test.ui.main;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.viewbinding.test.databinding.MainFragmentBinding;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;
public class MainFragment extends Fragment {
private MainFragmentBinding fragmentBinding;
private MainViewModel mViewModel;
public static MainFragment newInstance() {
return new MainFragment();
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
fragmentBinding = MainFragmentBinding.inflate(getLayoutInflater());