一 Android入门基础:从这里开始

gradle介绍:

Android Studio使用Gradle 编译运行Android工程. 工程的每个模块以及整个工程都有一个build.gradle文件。通常你只需要关注模块的build.gradle文件,该文件存放编译依赖设置,包括defaultConfig设置:

  • compiledSdkVersion 是我们的应用将要编译的目标Android版本,此处默认为你的SDK已安装的最新Android版本(如果你没有安装一个可用Android版本,就要先用SDK Manager来完成安装),我们仍然可以使用较老的版本编译项目,但把该值设为最新版本,可以使用Android的最新特性,同时可以在最新的设备上优化应用来提高用户体验。
  • applicationId 创建新项目时指定的包名。
  • minSdkVersion 创建项目时指定的最低SDK版本,是新建应用支持的最低SDK版本。
  • targetSdkVersion 表示你测试过你的应用支持的最高Android版本(同样用API level表示).当Android发布最新版本后,我们应该在最新版本的Android测试自己的应用同时更新target sdk到Android最新版本,以便充分利用Android新版本的特性。更多知识,请阅读Supporting Different Platform Versions
    apply plugin: 'com.android.application'
    
    android {
    compileSdkVersion 28
    defaultConfig {
    applicationId "com.example.panzq.myapplication"
    minSdkVersion 23
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
    release {
    minifyEnabled false
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    }
    } dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.+'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    }

    ####待补充》》》

ActionBar

所有的操作按钮和 action overflow 中其他可用的条目都被定义在 menu资源 的 XML 文件中。通过在项目的 res/menu 目录中新增一个 XML 文件来为 action bar 添加操作。

为想要添加到 action bar 中的每个条目添加一个 <item> 元素。例如:

res/menu/main_activity_actions.xml

<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 搜索, 应该作为动作按钮展示-->
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
app:showAsAction="ifRoom" />
<!-- 设置, 在溢出菜单中展示 -->
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
app:showAsAction="never" />
</menu>

要为 action bar 布局菜单条目,就要在 activity 中实现 onCreateOptionsMenu() 回调方法来 inflate 菜单资源从而获取 Menu 对象。例如:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// 为ActionBar扩展菜单项
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.aciton, menu);
return super.onCreateOptionsMenu(menu);
}

效果变成

为操作添加响应事件:

当用户按下某一个操作按钮或者 action overflow 中的其他条目,系统将调用 activity 中onOptionsItemSelected()的回调方法。在该方法的实现里面调用MenuItemgetItemId()来判断哪个条目被按下 - 返回的 ID 会匹配我们声明对应的 <item> 元素中 android:id 属性的值。

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// 处理动作按钮的点击事件
switch (item.getItemId()) {
case R.id.action_search:
//openSearch();
Toast.makeText(MainActivity.this,"openSearch",Toast.LENGTH_SHORT).show();
return true;
case R.id.action_settings:
//openSettings();
Toast.makeText(MainActivity.this,"openSettings",Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

提供向上的导航 点击左侧箭头返回到指定的主Activity中

方法一

Manifest中注册Activity

<activity
android:name=".SecondActivity"
android:label="@string/title_activity_display_mesage"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"></meta-data>
</activity>
SecondActivity中
public class SecondActivity extends AppCompatActivity {

    private TextView tv_content;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_second);
String content = getIntent().getStringExtra("putExtra");
tv_content = (TextView) findViewById(R.id.tv_showcontent);
tv_content.setText(content);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// 如果你的minSdkVersion属性是11活更高, 应该这么用:
// getActionBar().setDisplayHomeAsUpEnabled(true);
}
}

自定义ActionBar

Android 包含两个基本的 activity 主题,这两个主题决定了 action bar 的颜色:

这些主题即可以被应用到 app 全局,也可以通过在 manifest 文件中设置 <application> 元素 或 <activity>元素的 android:theme 属性,对单一的 activity 进行设置。

例如:

<application android:theme="@android:style/Theme.AppCompat.Light" ... />
可以通过声明 activity 的主题为 Theme.AppCompat.Light.DarkActionBar 来达到如下效果:action bar 为dark,其他部分为light。
AndroidManifest.xml
android:theme="@style/CustomActionBarTheme"
res/values/themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 应用于程序或者活动的主题 -->
<style name="CustomActionBarTheme"
parent="@style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">@style/MyActionBar</item>
</style> <!-- ActionBar 样式 -->
<style name="MyActionBar"
parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">@drawable/actionbar_background</item>
</style>
</resources>

res/drawable/actionbar_background.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010 Johan Nilsson <http://markupartist.com> Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="@color/actionbar_background_start"
android:endColor="@color/actionbar_background_end"
android:angle="-90" />
</shape>

自定义文本颜色

res/values/themes.xml


												

android入门小结一的更多相关文章

  1. Android入门(十二)SQLite事务、升级数据库

    原文链接:http://www.orlion.ga/610/ 一.事务 SQLite支持事务,看一下Android如何使用事务:比如 Book表中的数据都已经很老了,现在准备全部废弃掉替换成新数据,可 ...

  2. 【转】Xamarin.Android 入门之:Xamarin+vs2015 环境搭建

    Xamarin.Android 入门之:Xamarin+vs2015 环境搭建   一.前言 此篇博客主要写了如何使用搭建xamarin开发的环境,防止我自己万一哪天电脑重装系统了,可以直接看这篇博客 ...

  3. android 入门 006(sqlite增删改查)

    android 入门 006(sqlite增删改查) package cn.rfvip.feb_14_2_sqlite; import android.content.Context; import ...

  4. android 入门 005(登录记住)

    android 入门 005(登录记住) <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android ...

  5. Android入门:绑定本地服务

    一.绑定服务介绍   前面文章中讲过一般的通过startService开启的服务,当访问者关闭时,服务仍然存在: 但是如果存在这样一种情况:访问者需要与服务进行通信,则我们需要将访问者与服务进行绑定: ...

  6. Android入门视频推荐

      marschen老师的Android入门视频推荐网址: 1.Android应用程序开发视频教程(重制版)第一季 2.Android应用开发视频教程(重制版)第二季 2.marschen老师的个人微 ...

  7. Android入门教程之我见

    真正的从安卓入门学习到实际工作也差不多一年时间了,也做了几个项目.在这期间经历了一开始学习Android的基本知识后仍旧无从下手,不知道如何开始开发一个app,到现在也开始学会注意Android架构的 ...

  8. 小猪的Android入门之路 Day 3 - part 3

    小猪的Android入门之路 Day 3 - part 3 各种UI组件的学习 Part 3 本节引言: 在前面两个部分中我们对Android中一些比較经常使用的基本组件进行了一个了解, part 1 ...

  9. Android入门1:使用VideoView和MediController播放视频

    最近在搞Android,入门曲线还是挺陡峭的,主要还是自己对Java的理解不够深入.前后学习了几天,把最近学习到的一些知识点总结归纳一下,正所谓温故而知新. 目前想搞一个禁播视频站,主要内容都是一些大 ...

随机推荐

  1. MySql数据库学习笔记(1)

    MySql数据库 下载地址 https://dev.mysql.com/downloads/mysql/5.1.html#downloads 连接到本机上的MYSQL mysql -u root -p ...

  2. 【mmall】Jackson相关知识点

    Jackson fasterxml和codehaus的区别 (fasterxml vs. codehaus) http://blog.csdn.net/clementad/article/detail ...

  3. tomcat BIO / NIO

    BIO NIO ref https://www.jianshu.com/p/76ff17bc6dea http://gearever.iteye.com/blog/1841586

  4. 《jQuery精品教程视频》视频目录

    \day01\03-视频\02-使用js的缺点.avi; \day01\03-视频\03-jQuery初体验.avi; \day01\03-视频\04-什么是jQuery.avi; \day01\03 ...

  5. javascript编程基础1

    1,javascript能干什么? 直接写入html中: <script> document.write("<h1>这是一级标题</h1>") ...

  6. oracle-----视图/物化视图

    什么是视图 视图(view),也称虚表, 不占用物理空间,这个也是相对概念,因为视图本身的定义语句还是要存储在数据字典里的. 视图只有逻辑定义.每次使用的时候,只是重新执行SQL. 视图是从一个或多个 ...

  7. Vue & webpack

    在webpack构建的项目中使用vue进行开发 在入口文件中 import Vue from 'vue' 导入的构造函数,功能不完整,只提供了runtime-only的方式,并没有提供网页中那样的使用 ...

  8. Python运维开发基础05-语法基础【转】

    上节作业回顾(讲解+温习90分钟) #!/usr/bin/env python # -*- coding:utf-8 -*- # author:Mr.chen import os,time Tag = ...

  9. OninitDialog与OnCreate两个消息有何区别

    WM_INITDIALOGThe WM_INITDIALOG message is sent to the dialog box procedure immediately before a dial ...

  10. python3之模块SMTP协议客户端与email邮件MIME对象

    转载自https://www.cnblogs.com/zhangxinqi/p/9113859.html 阅读目录 1.smtplib模块的常用类与方法 2.处理邮件MIME 3.实例 (1)使用HT ...