📜  如何在Android中折叠工具栏布局?

📅  最后修改于: 2021-05-13 18:14:37             🧑  作者: Mango

在本文中,我们将创建令人着迷且非常有用的CollapsingToolbar应用程序。 CollapsingToolbarLayout提供了在工具栏标题文本被展开或收缩时调整其大小的功能。下面给出了一个示例GIF,以了解CollapsingToolbarLayout的外观。  

分步实施

步骤1:创建一个新项目

要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。

步骤2:添加设计支持库

如果您使用的是旧版Android Studio,请转到build.gradle文件并添加以下依赖项。较新版本的Android Studio默认包含此依赖项,因此,如果您使用的是最新版本,则无需添加它。

步骤3:新增图片

复制要用作工具栏背景图像的图像,然后将其粘贴到drawable文件夹中。我们将在activity_main.xml文件中使用此图像。

步骤4:使用字符串.xml文件

导航到res> values>字符串.xml ,然后在activity_main.xml文件中添加一个长文本,稍后我们将使用它。在这里,我们使用Lorem Ipsum文本的一部分您可以添加任何要使用的文本。

XML

    GFG | CollapseToolbar
    With the idea of imparting programming 
      knowledge, Mr. Sandeep Jain, an IIT Roorkee alumnus started a 
      dream, GeeksforGeeks. Whether programming excites you or you feel 
      stifled, wondering how to prepare for interview questions or how to
      ace data structures and algorithms, GeeksforGeeks is a one-stop solution.
      With every tick of time, we are adding arrows in our quiver. From articles 
      on various computer science subjects to programming problems for practice,
      from basic to premium courses, from technologies to entrance examinations, 
      we have been building ample content with superior quality. In a short span, 
      we have built a community of 1 Million+ Geeks around the world, 20,000+ 
      Contributors and 500+ Campus Ambassadors in various colleges across the nation.
      Our success stories include a lot of students who benefitted in their placements 
      and landed jobs at tech giants. Our vision is to build a gigantic network of 
      geeks and we are only a fraction of it yet.
  


XML

  


  
    
    
  
        
        
  
            
            
  
            
            
  
        
  
    
  
    
    
          
        
        
    
  


步骤5:使用activity_main.xml文件

导航到应用程序> res>布局> activity_main.xml,然后将以下代码添加到该文件中。以下是activity_main.xml文件的代码。

XML格式


  


  
    
    
  
        
        
  
            
            
  
            
            
  
        
  
    
  
    
    
          
        
        
    
  

输出:

想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处前往由我们的专家精心策划的指南,以使您立即做好行业准备!