Support customize layout in CollapsingToolbarBaseActivity

We introduced the collapsing toolbar on S. To enable this feature needs
to correspondingly adjust the theme that the activity uses. However
these changes might cause some mainline module apps get crashed when
running on R. A customize layout is allowed to use for some cases that
need to roll back to the layout used in R.

Bug: 192395187
Test: visual verified
Change-Id: I9b409842198a2d52a6440cdd9ad68268293681f2
This commit is contained in:
Mill Chen
2021-07-14 04:00:11 +08:00
parent b5d478b4cc
commit 4fdb7c9f26
2 changed files with 25 additions and 2 deletions

View File

@@ -18,6 +18,7 @@ android_library {
"androidx.core_core",
"com.google.android.material_material",
"SettingsLibSettingsTransition",
"SettingsLibUtils",
],
sdk_version: "system_current",
min_sdk_version: "29",

View File

@@ -28,6 +28,8 @@ import androidx.annotation.Nullable;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.fragment.app.FragmentActivity;
import com.android.settingslib.utils.BuildCompatUtils;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.android.material.resources.TextAppearanceConfig;
@@ -44,10 +46,15 @@ public class CollapsingToolbarBaseActivity extends FragmentActivity {
private CollapsingToolbarLayout mCollapsingToolbarLayout;
@Nullable
private AppBarLayout mAppBarLayout;
private int mCustomizeLayoutResId = 0;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (mCustomizeLayoutResId > 0 && !BuildCompatUtils.isAtLeastS()) {
super.setContentView(mCustomizeLayoutResId);
return;
}
// Force loading font synchronously for collapsing toolbar layout
TextAppearanceConfig.setShouldLoadFontSynchronously(true);
super.setContentView(R.layout.collapsing_toolbar_base_layout);
@@ -81,12 +88,27 @@ public class CollapsingToolbarBaseActivity extends FragmentActivity {
@Override
public void setContentView(View view) {
((ViewGroup) findViewById(R.id.content_frame)).addView(view);
final ViewGroup parent = findViewById(R.id.content_frame);
if (parent != null) {
parent.addView(view);
}
}
@Override
public void setContentView(View view, ViewGroup.LayoutParams params) {
((ViewGroup) findViewById(R.id.content_frame)).addView(view, params);
final ViewGroup parent = findViewById(R.id.content_frame);
if (parent != null) {
parent.addView(view, params);
}
}
/**
* This method allows an activity to replace the default layout with a customize layout. Notice
* that it will no longer apply the features being provided by this class when this method
* gets called.
*/
protected void setCustomizeContentView(int layoutResId) {
mCustomizeLayoutResId = layoutResId;
}
@Override