Android CollapsingToolbarLayout 工作不正常
Android CollapsingToolbarLayout works wrong
我使用 CoordinatorLayout
、AppBarLayout
、CollapsingToolbarLayout
和 Toolbar
进行布局。我用 drawerLayout
添加片段,用 collapsingToolbarLayout.setTitle("title")
添加 setTitle
片段。
但是有的页面运行良好,有的页面运行不正常。
我不知道为什么。谁能帮帮我?
编辑:
我发现用 CollapsingToolbarLayout
替换片段时会发生错误 collapsed.
xml这里
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="192dp"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbarLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.ContentFrameLayout
android:id="@+id/contents_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
从未对 appbarlayout 的值进行硬编码,使其包装内容以供参考,只需查看下面的 XML 代码即可。
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
android:background="@color/color_ffffff"
tools:context=".activity.profile.ProfileActivity">>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/transaparent_color">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="?attr/actionBarSize">
<include layout="@layout/myparallaxview" />
</FrameLayout>
<include layout="@layout/toolbar" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/color_ffffff"
android:orientation="vertical">
<android.support.v7.widget.AppCompatTextView
android:id="@+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="@dimen/dimen_15"
android:text="Chine Owhaigi"
android:textColor="@color/edit_text_color"
android:textSize="@dimen/dimen_text_16"
app:decorTypefaceAsset="@string/robotoRegular" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/user_contact_no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="@dimen/dimen_5"
android:text="7080644084"
android:textColor="@color/rechcolor"
android:textSize="@dimen/dimen_text_16"
app:decorTypefaceAsset="@string/robotoRegular" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dimen_25">
<RelativeLayout
android:layout_width="245dp"
android:layout_height="150dp"
android:layout_gravity="center">
<android.support.v7.widget.AppCompatImageView
android:id="@+id/profile_card"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/account_detail_card" />
<android.support.v7.widget.AppCompatImageView
android:id="@+id/add_money_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="@drawable/edit_account" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|bottom"
android:layout_marginBottom="@dimen/dimen_40"
android:paddingRight="@dimen/dimen_20"
android:orientation="vertical">
<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/ecashBalance"
android:textColor="@color/color_ffffff"
android:textSize="@dimen/dimen_text_13"
app:decorTypefaceAsset="@string/robotoRegular" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/cash_balance_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="2000000"
android:textColor="@color/color_ffffff"
android:textSize="@dimen/dimen_text_33"
app:decorTypefaceAsset="@string/robotoMedium" />
</LinearLayout>
</FrameLayout>
<android.support.v7.widget.AppCompatTextView
android:id="@+id/edit_saved_cards"
android:layout_width="match_parent"
android:layout_height="@dimen/dimen_60"
android:layout_marginTop="@dimen/dimen_30"
android:background="@color/color_f5f6f8"
android:gravity="center"
android:text="@string/saved_cards"
android:textColor="@color/edit_text_color"
android:textSize="@dimen/dimen_text_16"
app:decorTypefaceAsset="@string/robotoRegular" />
<include layout="@layout/profile_seprator" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/edit_pin"
android:layout_width="match_parent"
android:layout_height="@dimen/dimen_60"
android:background="@color/color_f5f6f8"
android:gravity="center"
android:text="@string/reset_mpin"
android:textColor="@color/edit_text_color"
android:textSize="@dimen/dimen_text_16"
app:decorTypefaceAsset="@string/robotoRegular" />
<include layout="@layout/profile_seprator" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/edit_profile"
android:layout_width="match_parent"
android:layout_height="@dimen/dimen_60"
android:background="@color/color_f5f6f8"
android:gravity="center"
android:text="@string/edit_profile"
android:textColor="@color/edit_text_color"
android:textSize="@dimen/dimen_text_16"
app:decorTypefaceAsset="@string/robotoRegular" />
<include layout="@layout/profile_seprator" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
我按照你的代码做了这个,你可以参考一下。它会帮助你。
MainActivity.java
public class MainActivity extends AppCompatActivity {
private CollapsingToolbarLayout collapsingToolbarLayout;
private android.support.v7.widget.Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initToolbar();
initAllWidgets();
}
private void initToolbar() {
toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
private void initAllWidgets() {
//setting toolbar properties;
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//setup root layout
collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.toolbarLayout);
collapsingToolbarLayout.setTitle("Abdul Rizwan");
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="192dp"
>
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbarLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleTextAppearance="@style/CollapsableTitle"
app:titleTextColor="@color/colorAccent"
app:expandedTitleMarginStart="64dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.ContentFrameLayout
android:id="@+id/contents_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="CollapsableTitle" parent="@android:style/TextAppearance">
<item name="android:textColor">@color/colorAccent</item>
<item name="android:textSize">@dimen/collapsable_title_size</item>
</style>
<dimen name="collapsable_title_size">26dp</dimen>
color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我使用 CoordinatorLayout
、AppBarLayout
、CollapsingToolbarLayout
和 Toolbar
进行布局。我用 drawerLayout
添加片段,用 collapsingToolbarLayout.setTitle("title")
添加 setTitle
片段。
但是有的页面运行良好,有的页面运行不正常。
我不知道为什么。谁能帮帮我?
编辑:
我发现用 CollapsingToolbarLayout
替换片段时会发生错误 collapsed.
xml这里
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="192dp"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbarLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.ContentFrameLayout
android:id="@+id/contents_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
从未对 appbarlayout 的值进行硬编码,使其包装内容以供参考,只需查看下面的 XML 代码即可。
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
android:background="@color/color_ffffff"
tools:context=".activity.profile.ProfileActivity">>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/transaparent_color">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="?attr/actionBarSize">
<include layout="@layout/myparallaxview" />
</FrameLayout>
<include layout="@layout/toolbar" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/color_ffffff"
android:orientation="vertical">
<android.support.v7.widget.AppCompatTextView
android:id="@+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="@dimen/dimen_15"
android:text="Chine Owhaigi"
android:textColor="@color/edit_text_color"
android:textSize="@dimen/dimen_text_16"
app:decorTypefaceAsset="@string/robotoRegular" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/user_contact_no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="@dimen/dimen_5"
android:text="7080644084"
android:textColor="@color/rechcolor"
android:textSize="@dimen/dimen_text_16"
app:decorTypefaceAsset="@string/robotoRegular" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dimen_25">
<RelativeLayout
android:layout_width="245dp"
android:layout_height="150dp"
android:layout_gravity="center">
<android.support.v7.widget.AppCompatImageView
android:id="@+id/profile_card"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/account_detail_card" />
<android.support.v7.widget.AppCompatImageView
android:id="@+id/add_money_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="@drawable/edit_account" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|bottom"
android:layout_marginBottom="@dimen/dimen_40"
android:paddingRight="@dimen/dimen_20"
android:orientation="vertical">
<android.support.v7.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/ecashBalance"
android:textColor="@color/color_ffffff"
android:textSize="@dimen/dimen_text_13"
app:decorTypefaceAsset="@string/robotoRegular" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/cash_balance_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="2000000"
android:textColor="@color/color_ffffff"
android:textSize="@dimen/dimen_text_33"
app:decorTypefaceAsset="@string/robotoMedium" />
</LinearLayout>
</FrameLayout>
<android.support.v7.widget.AppCompatTextView
android:id="@+id/edit_saved_cards"
android:layout_width="match_parent"
android:layout_height="@dimen/dimen_60"
android:layout_marginTop="@dimen/dimen_30"
android:background="@color/color_f5f6f8"
android:gravity="center"
android:text="@string/saved_cards"
android:textColor="@color/edit_text_color"
android:textSize="@dimen/dimen_text_16"
app:decorTypefaceAsset="@string/robotoRegular" />
<include layout="@layout/profile_seprator" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/edit_pin"
android:layout_width="match_parent"
android:layout_height="@dimen/dimen_60"
android:background="@color/color_f5f6f8"
android:gravity="center"
android:text="@string/reset_mpin"
android:textColor="@color/edit_text_color"
android:textSize="@dimen/dimen_text_16"
app:decorTypefaceAsset="@string/robotoRegular" />
<include layout="@layout/profile_seprator" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/edit_profile"
android:layout_width="match_parent"
android:layout_height="@dimen/dimen_60"
android:background="@color/color_f5f6f8"
android:gravity="center"
android:text="@string/edit_profile"
android:textColor="@color/edit_text_color"
android:textSize="@dimen/dimen_text_16"
app:decorTypefaceAsset="@string/robotoRegular" />
<include layout="@layout/profile_seprator" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
我按照你的代码做了这个,你可以参考一下。它会帮助你。
MainActivity.java
public class MainActivity extends AppCompatActivity {
private CollapsingToolbarLayout collapsingToolbarLayout;
private android.support.v7.widget.Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initToolbar();
initAllWidgets();
}
private void initToolbar() {
toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
private void initAllWidgets() {
//setting toolbar properties;
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//setup root layout
collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.toolbarLayout);
collapsingToolbarLayout.setTitle("Abdul Rizwan");
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="192dp"
>
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbarLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleTextAppearance="@style/CollapsableTitle"
app:titleTextColor="@color/colorAccent"
app:expandedTitleMarginStart="64dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.ContentFrameLayout
android:id="@+id/contents_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="CollapsableTitle" parent="@android:style/TextAppearance">
<item name="android:textColor">@color/colorAccent</item>
<item name="android:textSize">@dimen/collapsable_title_size</item>
</style>
<dimen name="collapsable_title_size">26dp</dimen>
color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>