Android ToolBar的使用
来源:互联网 发布:皇室战争mac模拟器 编辑:程序博客网 时间:2024/05/21 12:45
转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/64130211
本文出自【赵彦军的博客】
前言
ToolBar的出现是为了替换之前的ActionBar的各种不灵活使用方式,相反,ToolBar的使用变得非常灵活,因为它可以让我们自由往里面添加子控件.低版本要使用的话,可以添加support-v7包.
要使用ToolBar,首先引入v7包
compile 'com.android.support:appcompat-v7:25.3.0'
还需要在使用 ToolBar 的 Activity 下面添加去掉 ActionBar 的主题 :
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
比如:
<activity android:name=".MainActivity" android:theme="@style/Theme.AppCompat.Light.NoActionBar" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter></activity>
如果没有添加去掉 ActionBar 的主题 , 会报异常:
ToolBar 的使用
创建 activity_main.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF" > <!-- android:layout_height="?attr/actionBarSize" 根据手机分辨率自动分配高度--> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:background="@color/colorAccent" android:layout_height="?attr/actionBarSize" > <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="个人中心" android:layout_centerInParent="true" android:textColor="#ffffff" android:textSize="20sp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" android:layout_alignParentRight="true" /> </RelativeLayout> </android.support.v7.widget.Toolbar></RelativeLayout>
MainActivity 的代码逻辑
package com.constraintlayout.app;import android.graphics.Color;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.support.v7.widget.Toolbar;import android.view.View;import android.widget.Toast;public class MainActivity extends AppCompatActivity { private Toolbar mToolbar ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mToolbar = (Toolbar) findViewById(R.id.toolbar); mToolbar.setTitleTextColor(Color.YELLOW); //取代原本的actionbar setSupportActionBar(mToolbar); //ToolBar里面还可以包含子控件 mToolbar.findViewById(R.id.tv1 ).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "点击自定义按钮", Toast.LENGTH_SHORT).show(); } }); }}
效果图:
0 0
- Android --Toolbar的使用
- android toolbar的使用
- android:ToolBar的使用
- android Toolbar的使用
- Android ToolBar的使用
- Android Toolbar的使用
- Android ToolBar的使用
- Android之toolbar的使用
- Android之Toolbar的使用
- Android v7 Toolbar的使用
- Android中Toolbar的使用
- android中toolbar的使用
- Android控件Toolbar的使用
- Android中Toolbar的使用
- android ToolBar 的简单使用
- Android中Toolbar的使用
- Android ToolBar的简单使用
- android studio-toolbar的使用
- APIJSON-以坚持和偏执,回敬傲慢和偏见
- Linux+Nginx+Asp.net Core及守护进程部署
- 微信API实例(OKHttp+PullToRefreshListView+WebView综合应用)
- Ant Colony Optimization
- struts2(二) 表单参数自动封装和参数类型自动转换
- Android ToolBar的使用
- 我看百度的问题
- C#---反射ConstructorInfo
- Spring MVC入门2——一个简单例子
- Opencv2.4.13 与Visual Studio2013 环境搭建配置
- 【转】如何管理好新接手的团队
- 平衡二叉树的判断-->后序遍历
- Docker
- oracle通过impdp导入不同表用户、不同表空间的数据