android入门小结一
一 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()的回调方法。在该方法的实现里面调用MenuItem的getItemId()来判断哪个条目被按下 - 返回的 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 的颜色:
- Theme.AppCompat,一个 “dark” 的主题
- Theme.AppCompat.Light,一个 “light” 的主题
这些主题即可以被应用到 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

浙公网安备 33010602011771号