赞
踩
从今天开始就学Android Studio 推荐模板 ,今天发的本应该昨天发的,可同学刚睡醒连变压插座都没查就直接插上了电饭锅,然后过载了停了一天电这大星期天的宿舍没电。。。。
今天下午来电了,瞅了瞅加上昨天看的皮毛,就按照模板敲出来了,又加了点东西,还不错。
忘了一件事添加上 添加 compile 'com.android.support:design:23.2.1'
代码部分主要分几大块:
1.content_layout.xml 这里面就是主要的显示界面了,就是新建后的Hello World 了。但是这个布局文件并不是Activity中创建时的关联的布局文件,而是将这个布局文件嵌到另一个布局文件。
2.activityu_layout.xml这个文件就是被嵌的布局文件,这个文件就是主要的Toolbar控件和FloatingActionButton控件的布局了,这里面的布局结构很重要。
3.menu.xml这个文件就是菜单文件了,主要是为嵌到Toolbar栏上的菜单。
4.MainActivity.java 这个就是实现各种监听的。
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- app:layout_behavior="@string/appbar_scrolling_view_behavior"
- tools:showIn="@layout/activity_main">
-
- <TextView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:textSize="30dp"
- android:text="@string/hejingzhoublog"
- android:id="@+id/textView" />
-
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="TestButton"
- android:textAllCaps="false"
- android:id="@+id/button"
- android:layout_alignTop="@+id/textView"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="197dp" />
-
-
- </RelativeLayout>

- <?xml version="1.0" encoding="utf-8"?>
- <android.support.design.widget.CoordinatorLayout 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"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:fitsSystemWindows="true"
- android:background="#cc33cc"
- tools:context="com.example.hejingzhou.floatbutton.MainActivity">
-
- <android.support.design.widget.AppBarLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:theme="@style/AppTheme">
-
- <android.support.v7.widget.Toolbar
- android:id="@+id/toolbar"
- android:layout_width="match_parent"
- android:layout_height="?attr/actionBarSize"
- android:background="#778899"
- android:popupTheme="@style/AppTheme" />
- </android.support.design.widget.AppBarLayout>
-
- <include layout="@layout/content_layout" />
-
- <android.support.design.widget.FloatingActionButton
- android:id="@+id/flotButton"
- android:layout_width="50dp"
- android:layout_height="50dp"
- app:backgroundTint="#ff87ffeb"
- app:rippleColor="#708090"
- android:layout_gravity="bottom|end"
- android:layout_margin="13dp"
- android:src="@drawable/fly1" />
-
- </android.support.design.widget.CoordinatorLayout>

- <?xml version="1.0" encoding="utf-8"?>
- <menu xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto">
-
- <!--
- 1.alaways:这个值会使菜单项一直显示在ActionBar上。
- 2.ifRoom:如果有足够的空间,这个值会使菜单显示在ActionBar上。
- 3.never:这个值菜单永远不会出现在ActionBar是。
- 4.withText:这个值使菜单和它的图标,菜单文本一起显示。
- android:orderInCategory ="80"actionbar里每个item的优先级,
- 值越大优先级越低,actionbar地方不够就会放到overflow中
- -->
- <item
- android:id="@+id/edit"
- android:title="编辑"
- android:orderInCategory ="60"
- android:icon="@drawable/bian"
- app:showAsAction="ifRoom"/>
- <!--
- 查找有个问题 就是虽然设置了ifRoom但是 还是进入了never菜单中
- -->
- <item
- android:id="@+id/find"
- android:title="查找"
- android:orderInCategory ="60"
- android:icon="@drawable/cha"
- app:showAsAction="ifRoom"/>
-
- <item
- android:id="@+id/setMenu"
- android:orderInCategory="100"
- android:title="个性设置"
- app:showAsAction="never"/>
-
- <item
- android:id="@+id/messageMenu"
- android:orderInCategory="100"
- android:title="我的信息"
- app:showAsAction="never"/>
-
- </menu>

- package com.example.hejingzhou.floatbutton;
-
- import android.support.design.widget.FloatingActionButton;
- import android.support.design.widget.Snackbar;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.support.v7.widget.Toolbar;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.Button;
- import android.widget.Toast;
-
- public class MainActivity extends AppCompatActivity {
- private String TAG = getClass().getSimpleName();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- Button buttonTest = (Button)findViewById(R.id.button);
- buttonTest.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast.makeText(MainActivity.this,"TestActivity",Toast.LENGTH_SHORT).show();
- }
- });
-
- //工具条 并不是标题栏
- Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
- toolbar.setNavigationIcon(R.drawable.fly1);
- toolbar.setTitle("主标题");
- toolbar.setLogo(R.drawable.xiao);
- toolbar.setSubtitle("副标题");
- Log.i(TAG, "" + toolbar.getContentInsetLeft());
- setSupportActionBar(toolbar);
- toolbar.setNavigationOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast.makeText(MainActivity.this,"单击了导航栏图标",Toast.LENGTH_SHORT).show();
- }
- });
-
-
- //浮动Button
- FloatingActionButton floatButton = (FloatingActionButton)findViewById(R.id.flotButton);
- floatButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Snackbar.make(v, "你点击了 FloatButton", Snackbar.LENGTH_SHORT)
- .setAction("Action", null).show();
- }
- });
- }
-
- //菜单
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.menu, menu);
- return true;
- }
-
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- int id = item.getItemId();
- if(id == R.id.setMenu){
- Toast.makeText(MainActivity.this,"选择了设置菜单",Toast.LENGTH_SHORT).show();
- }else if(id == R.id.messageMenu){
- Toast.makeText(MainActivity.this,"选择了个人信息",Toast.LENGTH_SHORT).show();
- }else if(id == R.id.find){
- Toast.makeText(MainActivity.this,"选择了查找",Toast.LENGTH_SHORT).show();
- }else if(id == R.id.edit) {
- Toast.makeText(MainActivity.this,"编辑信息",Toast.LENGTH_SHORT).show();;
- }
- return super.onOptionsItemSelected(item);
- }
-
- @Override
- public void setSupportActionBar(Toolbar toolbar) {
- toolbar.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast.makeText(MainActivity.this,"点击了Toolbar栏",Toast.LENGTH_SHORT).show();
- }
- });
- super.setSupportActionBar(toolbar);
- }
- }

- </pre><pre code_snippet_id="1626955" snippet_file_name="blog_20160328_6_5301894" name="code" class="java">/**
- * 设置NavigationIcon图标点击监听方法有两种
- * 1.setSupportActionBar(toolbar);
- * toolbar.setNavigationOnClickListener(this);
- * 方法1需要注意的是必须放在setSupportActionBar方法之后。
- *
- *2.在onOptionsItemSelected方法中判断
- * if(id==android.R.id.home){
- //Toast.makeText(MainActivity.this, "You Clicked Lock Icon", Toast.LENGTH_SHORT).show();
- }
- */
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。