px dp sp 的區(qū)別
px | 其實就是像素單位,比如我們通常說的手機分辨列表800*400都是px的單位 |
sp | 同dp相似,還會根據(jù)用戶的字體大小偏好來縮放 |
dp | 虛擬像素喂分,在不同的像素密度的設(shè)備上會自動適配 |
隱藏狀態(tài)欄任務(wù)欄
在api30之前
getWindow().getDecorView().setSystemUIVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN)
api30后
WindowCompat.setDecorFitsSystemWindows(window, false)
WindowInsetsControllerCompat(window, window.decorView).let {
it.hide(WindowInsetsCompat.Type.systemBars())
//it.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_BARS_BY_TOUCH
}
- androidx.core 依賴的版本至少1.5
設(shè)置導(dǎo)航欄姓迅,狀態(tài)欄顏色
window.statusBarColor = Color.TRANSPARENT
window.navigationBarColor = Color.TRANSPARENT
fragment 生命周期
onCreateView 與onActivityCreated 的區(qū)別
onCreateView
每次創(chuàng)建瓣俯、繪制該Fragment的View組件時回調(diào)該方法夹抗,F(xiàn)ragment將會顯示該方法返回的View組件外莲。
onActivityCreated
當Fragment所在的Activity被啟動完成后回調(diào)該方法。
- 保存view的狀態(tài)的時候需要用onActivityCreated
- 訪問父activity的view層的時候需要在onActivityCreated 方法里面做
- 如果view是靜態(tài)的兔朦,就可以在onCreateView之后取得view進行操作
- 如果view是動態(tài)的,則需要在onActivityCreated后find
1 靜態(tài)布局
1.1 View和ViewGroup
靜態(tài)布局都是由View和ViewGroup繼承而來磨确。
靜態(tài)布局樹狀圖
- View是所有的UI組件都要繼承并實現(xiàn)的沽甥,一個View要在屏幕上占據(jù)一塊矩形區(qū)域。
- ViewGroup是一個容器乏奥,可以將View添加進ViewGroup中摆舟,ViewGroup可以對其內(nèi)的View進行布局,ViewGroup可以添加其他的ViewGroup。
- 所有的控件都是從View繼承而來恨诱,ViewGroup是View的一個重要子類媳瞪,絕大多數(shù)布局都是從ViewGroup繼承而來。
1.3 六大布局
1.3.1 LinearLayout
line是線的意思照宝,linear是線性的意思蛇受,這個布局就是線性布局。只能橫著排厕鹃,或者只能豎著排兢仰。
關(guān)鍵屬性是orientation,這個是方向的意思剂碴。
這個有兩個選項 vertical把将,horizontal。是不是覺得太長記不住呢忆矛,有一個簡便的記憶方法察蹲,利用IDE的自動提示功能,只要記住第一個字母就可以了對吧催训,可以把h記作橫(heng)這樣就不會錯了洽议。
1.3.2 RelativeLayout
relative是相對的意思,這個布局是相對布局瞳腌。
相對布局就是依據(jù)父元素和某元素進行相對布局绞铃。
有三種類型的屬性:
屬性值是true或false
android:layout_centerHrizontal 水平居中
android:layout_centerVertical 垂直居中
android:layout_centerInparent 相對于父元素完全居中。
android:layout_alignParentBottom 位于父元素的下邊緣
android:layout_alignParentTop 位于父元素的上邊緣
android:layout_alignParentLeft 位于父元素的左邊緣
android:layout_alignParentRight 位于父元素的右邊緣
屬性值是"@id/*“
android:layout_below 在某元素的下方
android:layout_above 在某元素的上方
andorid:layout_toRightOf 在某元素的右方
android:layout_toLeftOf 在某元素的左方
android:layout_alignBottom 和某元素下方對齊
android:layout_alignTop 和某元素上方對齊
android:layout_alignRight 和某元素右方對齊
android:layout_alignLeft 和某元素左方對齊
屬性值是數(shù)值
android:layout_marginLeft 離某元素左邊緣的距離
android:layout_marginRight 離某元素右邊緣的距離
android:layout_marginTop 離某元素上邊緣的距離
android:layout_marginBottom 離某元素下邊緣的距離
上面這兩種是最常用的兩種布局嫂侍,基本能完成一些基本的布局了儿捧。
1.3.3 自定義布局
自定義是對ViewGroup進行繼承,一般要實現(xiàn)以下方法:
重寫onMeasure()方法對子View進行測量挑宠。在onMeasure中計算childView的測量值以及模式菲盾,以及設(shè)置自己的寬和高。
重寫onLayout()方法確定子View的位置各淀,對所有childView進行定位(設(shè)置childView的繪制區(qū)域)懒鉴。
自定義ViewGroup是很有必要的,因為當官方給出的空間無法滿足自己的需求時碎浇,就需要自己取定義一種布局了临谱。
1.3.4 FrameLayout
框架布局,第一個控件放在最底層的左上角奴璃,然后后面的控件在左上角一層一層的覆蓋上去悉默。類似于Ps圖層的樣子。
1.3.5 TableLayout
表格布局苟穆,顧名思義抄课,就是一個表格唱星。
它遵循著以下結(jié)構(gòu):
<TableLayout>
<TableRow>
<!-在這里填充第一行的元素->
</TableRow>
<TableRow>
<!-在這里填充第二行的元素->
</TableRow>
</TableLayout>
還有幾個重要屬性:
- 寫在TableLayout中的屬性
- android:stretchColumns 設(shè)置第幾列為伸展(0表示第一列)
- ndroid:shrinkColumns 設(shè)置第幾列為收縮
- android:collapseColumns 設(shè)置第幾列為隱藏
- 寫在TableRow里的控件里的屬性
- android:layout_column 設(shè)置控件在第幾列
- android:layout_span 設(shè)置控件能跨多少列
1.3.6 AbsoluteLayout
絕對布局,已經(jīng)被淘汰了的布局跟磨。
所有控件都要設(shè)置x,y坐標间聊,使用絕對的布局。
android:layout_x //控件x坐標
android:layout_y //控件y坐標
1.4 控件
1.4.1 android自帶控件
1.4.2 自定義控件
自定義控件有三種方法:
- 對現(xiàn)有控件進行擴展抵拘,繼承控件后重寫onDraw()方法哎榴,在回調(diào)父類方法前實現(xiàn)自己的邏輯。
- 通過組合來實現(xiàn)新的控件
- 重寫View來實現(xiàn)全新的控件
@Override
protected void onDraw(Canvas canvas){
//在回調(diào)之前實現(xiàn)自己邏輯仑濒。
super.onDraw(canvas);
//在回調(diào)之后實現(xiàn)自己邏輯叹话。
}
2 動態(tài)布局
2.1 用類修改xml
可以使用findViewById找到想要進行接管的控件,然后再修改其中的屬性墩瞳,然后達到動態(tài)的目的驼壶。
2.2 動態(tài)生成
首先創(chuàng)建一個布局管理器,然后使用setContentView()將布局管理器繪制出來,之后再用addView()方法加入控件元素
Drawable類及XMLDrawable的使用
一.性質(zhì)
可直接使用.png喉酌、.jpg热凹、.gif、9.png等圖片作為資源,也可使用多種XML文件作為資源泪电。(就是這些資源都能生成Drawable對象)般妙。并對XML文件作出相關(guān)處理
二.XMLDrawable
1.StateListDrawable(selector)
作用:StateListDrawable對象所顯示的Drawable對象會隨著目標組件狀態(tài)的改變而改變
組成:
根元素<selector/>,子元素<item/>
子元素<item/>的屬性:android:color或android:drawable
android:state_xxx:狀態(tài)
2.LayerDrawable(ps圖層)
作用:可包含一個Drawable數(shù)組相速,系統(tǒng)會按照Drawable對象的數(shù)組順序繪制碟渺,索引越大越被繪制在上層
根元素:<layer-list> 子元素:<item/>
子元素的屬性:android:drawable 作為LayerDrawable的Drawable對象
android:id 為Drawable對象指定標識符
android:buttom|top...等 指定Drawable的繪制位置
3.ShapeDrawable
作用:設(shè)置一個基本的幾何圖形(矩形、圓形突诬、線條燈)
根元素:<shape/>
根元素的屬性:android:shape=["rectangle"|"oval"|"line"|"ring"]
子元素:
<corners/>:設(shè)置整體或者四個邊角的弧度
<gradient/>:漸變(可選擇漸變的角度但必須是45的倍數(shù)苫拍,默認為0,漸變的中心點旺隙,漸變的類型绒极,漸變的半徑和開始和終止的顏色)
<padding/>:內(nèi)邊距 (可以控制四周的邊距)
<size/>:形狀的大小 (設(shè)置形狀的寬高)
<solid/>:單種顏色填充
<stroke/>:繪制邊框
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:dither=["true" | "false"] //將在位圖的像素配置與屏幕不同時(例如:ARGB 8888 位圖和 RGB 565 屏幕)啟用位圖的抖動;值為“false”時則停用抖動蔬捷。默認值為 true垄提。
android:shape=["rectangle" | "oval" | "line" | "ring"]//分別為矩形、橢圓周拐、線铡俐、環(huán)。默認為矩形rectangle
android:innerRadius="integer" // shape為ring時有效妥粟,內(nèi)環(huán)半徑
android:innerRadiusRatio="float" // shape為ring時有效高蜂,內(nèi)環(huán)的厚度比,即環(huán)的圖形寬度與內(nèi)環(huán)半徑的比例罕容,按照這個比例計算內(nèi)環(huán)半徑备恤,默認為3,可被innerRadius值覆蓋
android:thickness="integer" // shape為ring時有效锦秒,環(huán)的厚度
android:thicknessRatio="float" // shape為ring時有效露泊,環(huán)的厚度比,即環(huán)的圖形寬度與環(huán)的厚度的比例旅择,按照這個比例計算環(huán)的厚度惭笑,默認為9,可被thickness值覆蓋
android:tint="color" // 給shape著色
android:tintMode=["src_in" | "src_atop" | "src_over" | "add" | "multiply" | "screen"] // 著色類型
android:useLevel=["true" | "false"] // 較少用生真,一般設(shè)為false沉噩,否則圖形不顯示。為true時可在LevelListDrawable使用
android:visible=["true" | "false"] >
<!-- 圓角 -->
<corners
android:radius="integer" // 圓角半徑柱蟀,設(shè)置下面四個屬性時川蒙,對應(yīng)的位置屬性會被覆蓋
android:topLeftRadius="integer" // 左上角圓角半徑
android:topRightRadius="integer" // 右上角圓角半徑
android:bottomLeftRadius="integer" // 左下角圓角半徑
android:bottomRightRadius="integer" // 右下角圓角半徑
/>
<!-- 漸變 -->
<gradient
android:type=["linear" | "radial" | "sweep"]// 漸變類型,線性长已、放射性畜眨、掃描性;默認為線性
android:angle="integer" // 漸變角度术瓮,漸變類型為linear時有效康聂;默認為0,從左至右漸變胞四,角度逆時針方向計算恬汁,角度需要時45的整數(shù)倍數(shù)
android:centerColor="integer" // 漸變中間位置顏色
android:startColor="color" // 漸變開始位置顏色
android:endColor="color" // 漸變結(jié)束位置顏色
android:centerX="float" // 設(shè)置漸變中心的X坐標,取值區(qū)間[0,1]辜伟,默認為0.5氓侧,即中心位置
android:centerY="float" // 設(shè)置漸變中心的Y坐標,取值區(qū)間[0,1]游昼,默認為0.5甘苍,即中心位置
android:gradientRadius="integer" // type為放射性漸變radial時有效,漸變的半徑
android:useLevel=["true" | "false"] // 與shape中該屬性的一致
/>
<!-- 內(nèi)邊距 -->
<padding
android:left="integer" // 左邊距
android:top="integer" // 上邊距
android:right="integer" // 右邊距
android:bottom="integer" // 下邊距
/>
<!-- 大小 -->
<size
android:width="integer" // 圖形寬度
android:height="integer" // 圖形高度
/>
<!-- 填充 -->
<solid
android:color="color" // 圖形的填充色
/>
<!-- 描邊 -->
<stroke
android:width="integer" // 描邊的寬度
android:color="color" // 描邊的顏色
android:dashWidth="integer" // 虛線寬度
android:dashGap="integer" // 虛線間隔
/>
</shape>
- 可設(shè)置畫筆的顏色和粗細并設(shè)置每畫一條線的長度和間距且必須兩者都設(shè)置才有效
4.ClipDrawable
作用:從Drawable上截取一個"圖片片段"
根元素:<clip> 不使用子元素烘豌。
根元素屬性:
android:drawable: 選定Drawable對象
android:clipOrientation:指定截取方向
android:gravity:從什么地方開始截取
總結(jié):選定圖片并選擇方向與位置截取圖片
使用:從java中獲取ClipDrawable并用setLevel()改變截取大小 //setLevel()只能從0~10000
<!--res/drawable/test_clip.xml-->
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@mipmap/start"
android:clipOrientation="horizontal"
android:gravity="center">
</clip>
<!--res/layout/activity_main.xml-->
<LinearLayout
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:orientation="vertical"
tools:context="com.chen.android.test.MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/test_clip"/>
</LinearLayout>
/*實現(xiàn)圖片漸漸展開的效果*/
public class MainActivity extends AppCompatActivity {
int data = 0;
int what = 0X11;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView img = (ImageView)findViewById(R.id.imageView);
//ImageView.getDrawable()獲取的是當前控件里的圖片载庭,返回的是Drawable類型,還有說明Drawable對象可隨意變成子對象并調(diào)用子對象的方法
final ClipDrawable clipDrawable = (ClipDrawable)img.getDrawable();
//創(chuàng)建Handler等待計時器傳送的信息,使圖片擴展
final Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == what){
clipDrawable.setLevel(data);//擴大截取的圖片面積
data += 200;
}
}
};
//創(chuàng)建計時器
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
if (data >= 10000){
timer.cancel();
}
mHandler.obtainMessage(what).sendToTarget();
}
},0,300);
}
}
5.AnimationDrawable
簡介:放在res/anim下廊佩,支持逐幀動畫和補間動畫
根元素:<set> 根元素屬性:android:interpolator="參數(shù)"
linear_interpolator:勻速變換
accelerate_interpolar:加速變換
decelerate_interpolator:減速變換
android:shareInterpolator= "true|false" :是否讓資源的interpolator與根元素相同
android:duration="時間":定義持續(xù)時間
子元素(同樣可以設(shè)置duration)
<alpha>:設(shè)置開始和結(jié)束的透明度
<scale>:設(shè)置縮放的中心囚聚、開始的X,Y的尺寸和結(jié)束時X,Y的尺寸
<translate>:設(shè)置圖片的開始位置和結(jié)束位置進行位移
<rotate>:設(shè)置旋轉(zhuǎn)的中心标锄、開始的角度和結(jié)束時候的角度
注意:利用android:fillAfter="true|false":設(shè)置保留后的狀態(tài)(哪個狀態(tài)想保留就用這個顽铸,如果都像就放在set中)
使用:利用AnimationUtils的靜態(tài)方法loadAninmation(Context context,int resId)
<!--在res/anim/test_animtaion中-->
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:shareInterpolator="true"
android:fillAfter="true">
<alpha android:fromAlpha="50.0"
android:toAlpha="100.0"
/>
<scale android:pivotX="50%"
android:pivotY="50%"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="1.5"
android:toYScale="1.5"
android:duration="3000"
/>
<translate android:fromXDelta="30"
android:toXDelta="300"
android:fromYDelta="40"
android:toYDelta="90"
android:duration="3000"/>
</set>
/*實現(xiàn)動畫*/
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView img = (ImageView)findViewById(R.id.imageView);
/*利用工具類獲取對象*/
Animation animation= AnimationUtils.loadAnimation(this,R.anim.test_animation);
/*將動畫附加在圖片上*/
img.startAnimation(animation);
}
}