Android資源文件與層次式導(dǎo)航超詳細(xì)講解
Android 自帶的資源
字符串資源:
- 定義字符串資源在 <string.xml >;
- 在JAVA 中 使用字符串資源,通過 getResource().getString(R.string.moto) 獲取strings.xml 資源,動態(tài)方式加載到textView 文本框中
TextView textview = findViewById(R.id.moto); textview.setText(getResource().getString(R.string.moto));
2.顏色資源
# 透明度 RGB 透明度一般是4位數(shù)字
dimen 表示尺寸資源,設(shè)置格子的間距
drawable 資源:
圖片資源 2. stateListDrawable資源
*.9.png : 是Android 下面自動伸縮的圖片,設(shè)置png的可縮放區(qū)域,讓圖片中固定圖像不會失真, tool下面 有個 Draw-9-patch.bat
注意圖片時候不能使用大寫字母
SateListDrawable :
1.失去焦點(diǎn) 和 匯聚焦點(diǎn) 使用selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="#f60"></item>
</selector>
Action Bar
1.提供快捷功能但是節(jié)省布局空間,比如淘寶界面上 商品分類
隱藏和顯示Action bar:
1. 在AndroidMainfest.xml 對應(yīng)的activity 標(biāo)簽上添加一個 android:theme="@style/Theme.Appcompat.Light.NoActionBar"
之上的方式是靜態(tài)方式顯示控制 Action Bar
2. 動態(tài) 隱藏或者顯示 Action Bar
3. ActionBar actionBar = getSupportActionBar()
button.setOnClickeListener (new ViewObClick() {
public void onClick(View v ){
actionBar.hide();
}
})Action Item
- 定義菜單資源文件
- 在OnCreateOptionsMenu() 方法加載菜單資源文件
注意menu.xml 中 關(guān)鍵app:showAsAction這個控件 有 四個屬性
always, ifRoom, never 表示Action Item 是否會顯示在導(dǎo)航欄中,ifRoom 表示有的話可以顯示,沒有的話隱藏起來, 最后一個Action Item通常使用 never 角色:
package com.example.myapplication;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import androidx.appcompat.app.AppCompatActivity;
/**
* Action Item顯示
*/
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// 重寫這個方法 重新加載
MenuInflater inflater = getMenuInflater();
//解析menu
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
}<?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">
<!-- 添加隱藏的Action item -->
<item android:id="@+id/search"
android:icon="@drawable/search"
android:title="search"
app:showAsAction="always"
/>
</menu>Action View
在Action Bar 上面有搜索功能的組件 叫Action View
在Action Bar上面添一些Action view 搜索的組件
package com.example.myapplication;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import androidx.appcompat.app.AppCompatActivity;
/**
* 手勢方式滑動動畫
*/
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 隱藏的Action Bar 顯示標(biāo)題
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// 重寫這個方法 重新加載
MenuInflater inflater = getMenuInflater();
//解析menu
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
}<?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">
<!-- v7 中自帶的組件 -->
<!-- 搜索的 Action View -->
<item android:id= "@+id/search"
android:title="搜索"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always"
/>
</menu>總結(jié) 添加Action View
第一種: app:actionViewClass: -> 實(shí)現(xiàn)類(Android自帶,自定義)
第二種: app:actionLayout -> 布局文件 *.xml
實(shí)現(xiàn)層次式導(dǎo)航
- 布局界面創(chuàng)建FriendsActivity
- 判斷父Activity 是否為空, 不為空設(shè)置返回導(dǎo)航顯示
- 為FriendActity 配置父Activity (需要在Mainifiest.xml 添加說明父Activity)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".FriendsActivity"
android:label="朋友圈" <!-- 這里給添加一個Action item 是名字-- >
>
<!-- 指定當(dāng)前 activity 的父activity-->
<meta-data android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"></meta-data>
</activity>
</application>
</manifest>package com.example.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
/**
* 手勢方式滑動動畫
*/
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView= (ImageView) findViewById(R.id.imageView); //獲取朋友圈圖片
imageView.setOnClickListener(new View.OnClickListener() { //為圖片設(shè)置單擊事件
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,FriendsActivity.class); //創(chuàng)建Intent對象
startActivity(intent); //啟動Activity
}
});
}
}
<?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:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
tools:context=".MainActivity">
<!-- 添加朋友圈 ImageButton的 -->
<ImageButton
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/button"
android:scaleType="fitXY"
android:layout_marginTop="@dimen/marginTop"></ImageButton>
</RelativeLayout>package com.example.myapplication;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NavUtils;
public class FriendsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_friends);
//獲取父Activity 是否為空
if(NavUtils.getParentActivityName(FriendsActivity.this) != null){
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
}
<?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="@drawable/bg"
tools:context=".FriendsActivity">
</RelativeLayout>到此這篇關(guān)于Android資源文件與層次式導(dǎo)航超詳細(xì)講解的文章就介紹到這了,更多相關(guān)Android資源文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android開發(fā)之OkHttpUtils的具體使用方法
這篇文章主要介紹了Android開發(fā)之OkHttpUtils的具體使用方法,非常具有實(shí)用價值,需要的朋友可以參考下2017-08-08
Android自定義View實(shí)現(xiàn)支付寶支付成功-極速get花式Path炫酷動畫
這篇文章主要介紹了Android自定義View實(shí)現(xiàn)支付寶支付成功-極速get花式Path炫酷動畫的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-01-01
android操作SQLite增刪改減實(shí)現(xiàn)代碼
android操作SQLite增刪改減實(shí)現(xiàn)代碼,學(xué)習(xí)android的朋友可以參考下。2010-11-11
Android實(shí)現(xiàn)簡單音樂播放器(MediaPlayer)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡單音樂播放器MediaPlayer的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
Android自定義FloatingActionButton滑動行為只隱藏不出現(xiàn)的問題小結(jié)
這篇文章主要介紹了Android自定義FloatingActionButton滑動行為只隱藏不出現(xiàn)的問題小結(jié),需要的朋友可以參考下2017-01-01
Android如何讓W(xué)ebView中的HTML5頁面實(shí)現(xiàn)視頻全屏播放
最近在工作遇到一個需求,需要讓W(xué)ebView中的HTML5頁面實(shí)現(xiàn)視頻全屏播放的效果,通過查找相關(guān)的資料終于找到了解決的方法,所以想著分享給大家,所以本文介紹了關(guān)于Android如何讓W(xué)ebView中的HTML5頁面實(shí)現(xiàn)視頻全屏播放的相關(guān)資料,需要的朋友可以參考學(xué)習(xí)。2017-04-04

