学习Material Design总结(二)可折叠式标题栏

本文详细解析了FruitActivity的布局文件activity_fruit.xml,展示了如何使用CoordinatorLayout、AppBarLayout、CollapsingToolbarLayout等组件实现动态效果。同时,介绍了FruitActivity.java中的关键代码,包括如何设置Toolbar、处理Intent数据、加载图片以及响应菜单项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

具体的布局文件就是activity_fruit

<?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"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:fitsSystemWindows="true"
    >
    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_height="250dp"
        android:layout_width="match_parent"
        android:fitsSystemWindows="true"
        >
        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/fruit_image_view"
                android:scaleType="centerCrop"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax"/>
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar1"
                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_height="match_parent"
        android:layout_width="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="15dp"
                app:cardCornerRadius="4dp">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/friut_content_text"
                    android:layout_margin="10dp"/>

            </android.support.v7.widget.CardView>


        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>
    <android.support.design.widget.FloatingActionButton
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_margin="16dp"
        android:src="@drawable/activity_me_add1"
        app:layout_anchor="@id/appBar"
        app:layout_anchorGravity="bottom|end"
        />

</android.support.design.widget.CoordinatorLayout>

具体的代码是FruitActivity.java

public class FruitActivity extends AppCompatActivity {
    public static final String Fruit_Name="fruit_name";
    public static final String Fruit_ImageId="fruit_imageid";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int a=0;
        setContentView(R.layout.activity_fruit);
        Intent intent=getIntent();
        String fruitname=intent.getStringExtra(Fruit_Name);
        int imageid=intent.getIntExtra(Fruit_ImageId,0);
        Toolbar toolbar=(Toolbar) findViewById(R.id.toolbar1);
        CollapsingToolbarLayout collapsingToolbarLayout=(CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
        ImageView fruitiImageView=(ImageView)findViewById(R.id.fruit_image_view);
        TextView fruitConTextView=(TextView)findViewById(R.id.friut_content_text);
        setSupportActionBar(toolbar);
        ActionBar actionBar=getSupportActionBar();
        if(actionBar!=null){
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        collapsingToolbarLayout.setTitle(fruitname);
        Glide.with(this).load(imageid).into(fruitiImageView);
        String fruitContent=getFruitContent(fruitname);
        fruitConTextView.setText(fruitContent);
    }
    private String getFruitContent(String fruitname){
        StringBuilder content=new StringBuilder();
        for (int i=0;i<500;i++){
            content.append(fruitname);
        }
        return content.toString();

    }

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

    }


}

1.CoordinatorLayout
实现该效果需要在android5.0以上系统。
在这里插入图片描述.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值