android之可折叠式标题栏(CollapsingToolbarLayout)

1.CollapsingToolbarLayout是一个作用于Toolbar基础之上的布局,CollapsingToolbarLayout可以让Toolbar的效果

   变得更加丰富,CollapsingToolbarLayout是不能独立存在的,在设计的时候就被限定为只能作为AppBarLayout

   的直接子布局来使用,而AppBarLayout又必须是CoordinatorLayout的子布局。

   *app:contentScrim="?attr/colorPrimary":指定CollapsingToolbarLayout在趋于折叠状态以及折叠之后的背景色。

   *app:layout_scrollFlags="scroll|exitUntilCollapsed":scroll表示CollapsingToolbarLayout会随着内容的滚动一起

     滚动,exitUntilCollapsed表示当前CollapsingToolbarLayout随着滚动完成折叠之后就保留在界面上。

   *app:layout_collapseMode="parallax":制定当前控件在CollapsingToolbarLayout折叠过程中的折叠模式,其中

     Toolbar制定成pin,表示在折叠过程中位置始终保持不变,ImageView指定成parallax,表示会在折叠的过程中产

      生一定的错位偏移,这种模式效果非常好。

2.FloatingActionButton

    *app:layout_anchor="@id/appBar":指定一个锚点并将锚点设置为AppBarLayout,这样悬浮按钮就会出现在谁果

      标题栏的区域内,接着又使用app:layout_anchorGravity属性将悬浮按钮定位在标题栏区域的右下角。

3.充分利用系统状态栏空间

   * android:fitsSystemWindows="true":表示该控件会出现在系统状态栏里。必须在指定控件的所有父布局都设置

     才有效果。但是设置好了还没有用,还必须在程序的主题中将状态栏颜色指定成透明色才行。制定城透明色的

     方法很简单,在主题中将android:statusBarColor属性的值指定成@android:color/transparent就可以,但问题是

     android:statusBarColor属性从API21才有。解决方法为:

     *1.新建values-v21文件夹,然后新建styles文件

<resources>
    <style name="FruitActivityTheme" parent="AppTheme">
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>
</resources>

     *2.在values文件夹中的styles文件中添加,并在Ac中指定。由于values-v21目录是只有5.0及以上系统才会去读取

         因此5.0之前的系统无法识别,因此还需要对values/styles.xml文件进行修改。

<style name="FruitActivityTheme" parent="AppTheme"/>

3.布局代码

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:fitsSystemWindows="true"
        android:layout_width="match_parent"
        android:layout_height="250dp">
        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">
            <ImageView
                android:fitsSystemWindows="true"
                android:id="@+id/fruit_image_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"/>
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                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">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="15dp"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                android:layout_marginTop="35dp"
                app:cardCornerRadius="4dp">
                <TextView
                    android:id="@+id/fruit_content_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="10dp"/>
            </android.support.v7.widget.CardView>
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:src="@mipmap/ic_launcher"
        app:layout_anchor="@id/appBar"
        app:layout_anchorGravity="bottom|end"/>
</android.support.design.widget.CoordinatorLayout>

4.代码

public class FruitActivity extends AppCompatActivity {
    public static final String FRUIT_NAME="fruit_name";
    public static final String FRUIT_IMAGE_ID="fruit_image_id";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fruit);
        Intent intent=getIntent();
        String fruitName=intent.getStringExtra(FRUIT_NAME);
        int fruitNameId=intent.getIntExtra(FRUIT_IMAGE_ID,0);
        Toolbar toolbar=findViewById(R.id.toolbar);
        CollapsingToolbarLayout collapsingToolbarLayout=findViewById(R.id.collapsing_toolbar);
        ImageView fruitImageView=findViewById(R.id.fruit_image_view);
        TextView fruitContentText=findViewById(R.id.fruit_content_text);
        setSupportActionBar(toolbar);
        ActionBar actionBar=getSupportActionBar();
        if (actionBar!=null){
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        collapsingToolbarLayout.setTitle(fruitName);
        Glide.with(this).load(fruitNameId).into(fruitImageView);
        String fruitContent=generateFruitContent(fruitName);
        fruitContentText.setText(fruitContent);
    }

    private String generateFruitContent(String fruitName){
        StringBuilder fruitContent=new StringBuilder();
        for (int i=0;i<500;i++){
            fruitContent.append(fruitName);
        }
        return fruitContent.toString();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值