Android自定义ToolBar布局

本文介绍如何在Android应用中自定义Toolbar布局实现类似iOS的Tab切换功能。通过使用LayoutInflater.inflate方法,将自定义布局添加到Toolbar中,实现了左右切换的效果。文章提供了具体的布局文件和Activity代码示例。

在项目中,我们经常使用toolbar作为标题栏,并为之添加菜单选项。但是会出现这用情况:在主界面要实现类似于tab的切换功能,和IOS那种一样,左右切换,对于用toolbar布局来说,这就有点麻烦了。此时我们就需要来自定义toolbar的布局了,其实还是挺简单的,这里我们需要用到getLayoutInflater().inflate(int,ViewGroup)这个方法,此方法可以将制定的布局文件添加到viewgroup视图中,所以我们就可以用这个方法,把自定义的布局添加到我们的toolbar中,来代替原来的toolbar布局。老规矩,先来张效果图看看:结尾有实例下载的。

这就是仿的QQ那种tab切换效果。

实现步骤如下:

1、首先我们需要写好我们需要自定义的布局文件(toobar_button.xml)

 

[html] view plain copy

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="wrap_content"  
  3.     android:layout_height="wrap_content"  
  4.     android:background="@android:color/transparent">  
  5.   
  6.     <RelativeLayout  
  7.         android:layout_width="match_parent"  
  8.         android:layout_height="?attr/actionBarSize">  
  9.         <TextView  
  10.             android:id="@+id/tv_title"  
  11.             android:layout_width="wrap_content"  
  12.             android:layout_height="wrap_content"  
  13.             android:layout_centerVertical="true"  
  14.             android:layout_marginLeft="12dp"  
  15.             android:text="消息"  
  16.             android:textSize="18sp"  
  17.             android:visibility="gone"/>  
  18.         <LinearLayout  
  19.             android:id="@+id/ly_title"  
  20.             android:layout_width="wrap_content"  
  21.             android:layout_height="match_parent"  
  22.             android:orientation="horizontal"  
  23.             android:layout_centerInParent="true"  
  24.             android:gravity="center">  
  25.             <TextView  
  26.                 android:id="@+id/tv_msg"  
  27.                 android:layout_width="wrap_content"  
  28.                 android:layout_height="wrap_content"  
  29.                 android:layout_centerVertical="true"  
  30.                 android:gravity="right|center_vertical"  
  31.                 android:paddingRight="12dp"  
  32.                 android:text="消息"  
  33.                 android:textSize="14sp"  
  34.                 android:background="@drawable/bg_zhibo_selected"/>  
  35.   
  36.             <TextView  
  37.                 android:id="@+id/tv_phone"  
  38.                 android:layout_width="wrap_content"  
  39.                 android:layout_height="wrap_content"  
  40.                 android:gravity="left|center_vertical"  
  41.                 android:paddingLeft="12dp"  
  42.                 android:text="电话"  
  43.                 android:textSize="14sp"  
  44.                 android:background="@drawable/bg_gonghui_selected"/>  
  45.         </LinearLayout>  
  46.     </RelativeLayout>  
  47. </RelativeLayout>  

这个布局文件就是左右切换的tab布局文件,都很简单的。其中选中和未选中效果是设置的selector,这里就不在赘述了,相信大家都会使用的。
2、然后就是在activity中调用之(MainActivity.java)

 

 

[java] view plain copy

  1. package com.ywl5320.toolbartab;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.support.v7.widget.Toolbar;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.TextView;  
  9.   
  10. public class MainActivity extends Activity {  
  11.   
  12.     private Toolbar toolbar;  
  13.   
  14.     private TextView tvMsg;  
  15.     private TextView tvPhone;  
  16.     private TextView tvContent;  
  17.   
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.activity_main);  
  22.         toolbar = (Toolbar) findViewById(R.id.tool_bar);  
  23.         getLayoutInflater().inflate(R.layout.toobar_button, toolbar);//把自定义布局文件添加到toolbar中  
  24.   
  25.         tvMsg = (TextView) findViewById(R.id.tv_msg);  
  26.         tvPhone = (TextView) findViewById(R.id.tv_phone);  
  27.         tvContent = (TextView) findViewById(R.id.tv_content);  
  28.   
  29.         selectedToolbarItem(0);  
  30.         tvContent.setText("消息");  
  31.   
  32.         tvMsg.setOnClickListener(new OnClickListener() {  
  33.   
  34.             @Override  
  35.             public void onClick(View v) {  
  36.                 // TODO Auto-generated method stub  
  37.                 selectedToolbarItem(0);  
  38.                 tvContent.setText("消息");  
  39.             }  
  40.         });  
  41.   
  42.         tvPhone.setOnClickListener(new OnClickListener() {  
  43.   
  44.             @Override  
  45.             public void onClick(View v) {  
  46.                 // TODO Auto-generated method stub  
  47.                 selectedToolbarItem(1);  
  48.                 tvContent.setText("电话");  
  49.             }  
  50.         });  
  51.   
  52.     }  
  53.   
  54.     /** 
  55.      *设置选中的条目,选中状态和颜色 
  56.      * @param item 
  57.      *            0 :消息 1:电话 
  58.      */  
  59.     public void selectedToolbarItem(int item) {  
  60.         if (item == 0) {  
  61.             tvMsg.setSelected(true);  
  62.             tvPhone.setSelected(false);  
  63.   
  64.             tvMsg.setTextColor(getResources().getColor(R.color.default_font));  
  65.             tvPhone.setTextColor(getResources().getColor(R.color.font_white));  
  66.         } else if (item == 1) {  
  67.             tvMsg.setSelected(false);  
  68.             tvPhone.setSelected(true);  
  69.   
  70.             tvMsg.setTextColor(getResources().getColor(R.color.font_white));  
  71.             tvPhone.setTextColor(getResources().getColor(R.color.default_font));  
  72.         }  
  73.     }  
  74.   
  75. }  

save_snippets.png

getLayoutInflater().inflate(R.layout.toobar_button, toolbar);这就是核心语句,这样就把我们写好的自定义布局添加到了toolbar中,其他的都是一些控制逻辑,也是很简单的。这就完成了toolbar的自定义布局了。实例下载
 

 

转载于:https://my.oschina.net/u/1177694/blog/909093

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值