一 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. cygwin 安装包管理器 apt-cyg

    https://github.com/transcode-open/apt-cyg apt-cyg is a simple script. To install: lynx -source https ...

  2. python 函数名 、闭包 装饰器 day13

    1,函数名的使用. 函数名是函数的名字,本质就是变量,特殊的变量.函数名()加括号就是执行此函数. 1,单独打印函数名就是此函数的内存地址. def func1(): print(555) print ...

  3. markdown自动生成侧边栏TOC /目录

    http://blog.csdn.net/haleypku/article/details/51226704 此文可以只了解一下概念: http://i5ting.github.io/i5ting_z ...

  4. 最短路径&次短路径算法

    容易理解:https://blog.csdn.net/m0_37345402/article/details/76695930 https://blog.csdn.net/qq_36386435/ar ...

  5. nginx 开启静态 gzip 配合 Vue 构建

    在站点配置添加如下代码: location ~* \.(css|js)$ { gzip_static on; } 这是 nginx 的静态 gzip功能,会自动查找对应扩展名的文件,如果存在 gzip ...

  6. ASP.NET MVC - 安全、身份认证、角色授权和ASP.NET Identity

    ASP.NET MVC - 安全.身份认证.角色授权和ASP.NET Identity ASP.NET MVC内置的认证特性 AuthorizeAttribute特性(System.Web.Mvc)( ...

  7. C++ 引用变量

    int rats; int & rodents = rats; rats 和 rodents 可以互换,他们指向相同的值和内存单元.其实就是给rats取了别名rodents. 修改其中任意一个 ...

  8. Zookeeper客户端Curator基本API

    在使用zookeper的时候一般不使用原生的API,Curator,解决了很多Zookeeper客户端非常底层的细节开发工作,包括连接重连.反复注册Watcher和NodeExistsExceptio ...

  9. Spring Bean自动检测

    1-自动检测bean 需要用到<context:component-scan> 注意:a) 需要include进来xmlns:context命名空间:base-package指的是我们要扫 ...

  10. 网络爬虫re模块的findall()函数

    findall()函数匹配所有符合规律的内容,并以列表的形式返回结果. a = '"<div>指数' \ '</div>"' word = re.finda ...