Android开发日常笔记
如何在Android Studio添加本地aar包引用
如何在Android studio中,給android 項目添加外部lib引用。在android studio中,有兩種方式:一種是jar包,一種是帶資源文件的aar包,
- jar: 只包含了class文件與清單文件 ,不包含資源文件,如圖片等所有res中的文件。
- aar: 包含jar包和資源文件,如圖片等所有res中的文件
復制外部aar包到libs目錄下,在build.gradle下添加依賴
apply plugin: 'com.android.application'android {... }repositories {flatDir {dirs 'libs'} } dependencies {compile (name:'libname',ext:'aar') }editplus文件編碼批量轉換
editplus全部打開之后(打開為何種編碼不重要),選文檔(Document)菜單 –>文件編碼(File encoding) –> 批量轉換編碼(File encoding multiple),選中全部文件后轉碼,然后再全部保存
minSdkVersion、targetSdkVersion、maxSdkVersion、target API level四個數值到底有什么區別?
minSdkVersion與maxSdkVersion比較容易理解,就是在安裝程序的時候,如果目標設備的API版本小于minSdkVersion,或者大于maxSdkVersion,程序將無法安裝。一般來說沒有必要設置maxSdkVersion。
targetSdkVersion相對復雜一些,如果設置了此屬性,那么在程序執行時,如果目標設備的API版本正好等于此數值,他會告訴Android平臺:此程序在此版本已經經過充分測,沒有問題。不必為此程序開啟兼容性檢查判斷的工作了。也就是說,如果targetSdkVersion與目標設備的API版本相同時,運行效率可能會高一些。但是,這個設置僅僅是一個聲明、一個通知,不會有太實質的作用,比如說,使用了targetSdkVersion這個SDK版本中的一個特性,但是這個特性在低版本中是不支持的,那么在低版本的API設備上運行程序時,可能會報錯:java.lang.VerifyError。也就是說,此屬性不會幫你解決兼容性的測試問題。你至少需要在minSdkVersion這個版本上將程序完整的跑一遍來確定兼容性是沒有問題的。
在default.properties中的target是指在編譯的時候使用哪個版本的API進行編譯。
綜上,上面的四個值其實是作用于不同的時期:target API level是在編譯的時候起作用,用于指定使用哪個API版本(SDK版本)進行編譯。minSdkVersion和maxSdkVersion是在程序安裝的時候起作用,用于指定哪些版本的設備可以安裝此應用。targetSdkVersion是在程序運行的時候起作用,用于提高指定版本的設備上程序運行體驗。這四個數值在程序編譯時也沒有嚴格的檢查,比如說,你可以將minSdkVersion設置的比maxSdkVersion還大,他會自動忽略掉錯誤的maxSdkVersion。
System
System.arraycopy(); System.currentTimeMillis(); SystemClock.sleep();自定義控件
textView.setY(); AppCompatTextView#setSupportBackgroundTintList(ColorStateList.valueOf(color));SparseArray
- put()
- indexOfKey()
- indexOfValue()
- keyAt()
- valueAt()
TypedValue
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80, getResources().getDisplayMetrics());setSoftInputMode
((Activity) mContext).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);android.text.format.dateformat
http://blog.csdn.net/jason_wks/article/details/7412562
注冊上下文菜單
registerForContextMenu(View view); onContextItemSelected(MenuItem item); onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);BaseColumns
BaseColumns接口有兩個常量:1.總行數_count 2.每行的獨特的_ID
實現BaseColumns接口以后,內部類就可以繼承一個主鍵字段_ID,這也是很多android里的類所需要的,比如游標適配器(cursor adaptor)。這雖然不是必須,但是可以使數據庫和android的框架協調工作。
LayoutInflater
inflate(int resource, ViewGroup root, boolean attachToRoot);1、Android設置全屏的三種方式
1.1 代碼設置
在setContentView()之前執行
requestWindowFeature(Window.FEATURE_NO_TITLE);//隱藏標題欄 int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN; getWindow().setFlags(flag ,flag );//隱藏狀態欄 setContentView(R.layout.main_activity);當MainActivity繼承的不是Activity,而是AppCompatActivity,而且主題不是NoActionBar的時候,調用requestWindowFeature(Window.FEATURE_NO_TITLE)無法隱藏標題欄,需要調用一下代碼隱藏ActionBar
if (getSupportActionBar() != null){getSupportActionBar().hide(); }1.2 調用Android自帶的Theme
直接在AndroidManifest.xml中需要全屏顯示的Activity屬性中添加
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"1.3 自己定義全屏Theme
在style.xml文件中定義theme(如果沒有style.xml,在res/values目錄下創建)
<resources> <style name="Theme.NoTitle_FullScreen"> <item name="android:windowNoTitle">true</item> <item name="android:windowFullscreen">true</item> </style> </resources>直接在AndroidManifest.xml中需要全屏顯示的Activity屬性中添加
android:theme="@style/Theme.NoTitle_FullScree"橫屏
@Override protected void onResume() {if(getRequestedOrientation()!=ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);}super.onResume(); } android:screenOrientation="portrait"分享
分享應用下載鏈接
//分享:https://play.google.com/store/apps/details?id=com.tencent.mobileqq Intent intent = new Intent(); intent.setAction(Intent.ACTION_SEND); intent.setType("text/plain"); intent.putExtra(Intent.EXTRA_SUBJECT,"分享"); intent.putExtra(Intent.EXTRA_TEXT,"下載地址:" + "https://play.google.com/store/apps/details?id=" + "packagename"); startActivity(Intent.createChooser(intent,"分享應用"));位置分享
Intent intent = new Intent(); intent.setAction(Intent.ACTION_SEND); intent.setType("text/plain"); intent.putExtra(Intent.EXTRA_TEXT,位置url); startActivity(Intent.createChooser(intent,"位置分享"));硬件加速
四個硬件加速級別
Application
<application android:hardwareAccelerated="true"> ... </application>Activity
<activity android:hardwareAccelerated="true" />Window
getWindow().setFlags( WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);View
view.setLayerType(View.LAYER_TYPE_SOFTWARE,null);判定一個View是否能被硬加速
View.isHardwareAccelerated();//如果View附加到一個硬加速的window上就返回true Canvas.isHardwareAccelerated();//如果Canvas被硬加速了就返回true.使用系統的主題顏色
app:tabTextColor="?android:attr/textColorTertiaryInverse"新聞客戶端
記錄已讀狀態
lvList.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {System.out.println("被點擊:" + position);// 35311,34221,34234,34342// 在本地記錄已讀狀態String ids = PrefUtils.getString(mActivity, "read_ids", "");String readId = mNewsList.get(position).id;if (!ids.contains(readId)) {ids = ids + readId + ",";PrefUtils.setString(mActivity, "read_ids", ids);}// mNewsAdapter.notifyDataSetChanged();changeReadState(view);// 實現局部界面刷新, 這個view就是被點擊的item布局對象// 跳轉新聞詳情頁Intent intent = new Intent();intent.setClass(mActivity, NewsDetailActivity.class);intent.putExtra("url", mNewsList.get(position).url);mActivity.startActivity(intent);}});/*** 改變已讀新聞的顏色*/private void changeReadState(View view) {TextView tvTitle = (TextView) view.findViewById(R.id.tv_title);tvTitle.setTextColor(Color.GRAY);}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder holder;if (convertView == null) {convertView = View.inflate(mActivity, R.layout.list_news_item,null);holder = new ViewHolder();holder.ivPic = (ImageView) convertView.findViewById(R.id.iv_pic);holder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);holder.tvDate = (TextView) convertView.findViewById(R.id.tv_date);convertView.setTag(holder);} else {holder = (ViewHolder) convertView.getTag();}TabNewsData item = getItem(position);holder.tvTitle.setText(item.title);holder.tvDate.setText(item.pubdate);utils.display(holder.ivPic, item.listimage);String ids = PrefUtils.getString(mActivity, "read_ids", "");if (ids.contains(getItem(position).id)) {holder.tvTitle.setTextColor(Color.GRAY);} else {holder.tvTitle.setTextColor(Color.BLACK);}return convertView;}ProgressBar
<ProgressBar android:id="@+id/loading_progress"android:layout_width="@dimen/dp_22"android:layout_height="@dimen/dp_22"android:layout_marginRight="@dimen/dp_4"android:indeterminateDrawable="@drawable/sample_footer_loading_progress" />sample_footer_loading_progress
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"><item><rotate android:drawable="@drawable/sample_footer_loading"android:duration="500"android:fromDegrees="0.0"android:pivotX="50.0%"android:pivotY="50.0%"android:toDegrees="360.0" /></item></layer-list>數據存儲和數據傳輸單位
字節是由8個位所組成,可代表一個字符(A~Z)、數字(0~9)、或符號(,.?!%&+-*/),是內存儲存數據的基本單位
- 1 byte = 8 bit
- 1 KB = 1024 bytes = 210 bytes
- 1 MB = 1024 KB = 220 bytes
- 1 GB = 1024 MB = 230 bytes
位:“位(bit)”是電子計算機中最小的數據單位。每一位的狀態只能是0或1。
字節:8個二進制位構成1個“字節(Byte)”,它是存儲空間的基本計量單位。1個字節可以儲存1個英文字母或者半個漢字,換句話說,1個漢字占據2個字節的存儲空間。
字:“字”由若干個字節構成,字的位數叫做字長,不同檔次的機器有不同的字長。例如一臺8位機,它的1個字就等于1個字節,字長為8位。如果是一臺16位機,那么,它的1個字就由2個字節構成,字長為16位。字是計算機進行數據處理和運算的單位。
KB:在一般的計量單位中,通常K表示1000。例如:1公里= 1000米,經常被寫為1km;1公斤=1000克,寫為1kg。同樣K在二進制中也有類似的含義。只是這時K表示1024,也就是2的10次 方。1KB表示1K個Byte,也就是1024個字節。
MB:計量單位中的M(兆)是10的6次方,見到M自然想起要在該數值的后邊續上六個0,即擴大一百萬倍。在二進制中,MB也表示到了百萬級的數量級,但1MB不正好等于1000000字節,而是1048576字節,即 1MB = 2E+20 Bytes = 1048576Bytes。
計算機系統中的數據的計量單位。
在標準10進制公制度量系統中,倍率關系如下所示
- kilo (k)* = 103 = 1,000 thousand 千
- mega (M) = 106 = 1,000,000 million 百萬
- giga (G) = 10 9 = 1,000,000,000 billion 十億
- tera (T) = 10 12 = 1,000,000,000,000 trillion 萬億
在公制系統中, “k” 或者 “kilo” 前綴只使用小寫字母
在計算機/通訊行業中,計算數據傳送速度也使用每秒傳送公制數據量來計算
- 1 bit (b) = 0 or 1 = one binary digit 一個二進制位元
- 1 kilobit(kb)=103 bits = 1,000 bits 一千位元
- 1 Megabit(Mb)=106 bits = 1,000,000 bits 一百萬位元
- 1 Gigabit(Gb)=109 bits = 1,000,000,000 bits 一萬億位元
根據進制規定,傳送速度可以有兩種表示方法 bps 和 Bps,但是他們是有嚴格區別。Bps中的 B 使用的是二進制系統中的Byte字節 ,bps中的 b 是十進制系統中的位元。
在我們常說的56K撥號,100M局域網都是bps計量,當用于軟件下載時,下載工具一般又以Bps計算,所以它們之間有 8 bit=1 Byte 的換算關系,那么56Kbps撥號極限下載速度是 56Kbps/8=7KBps 每秒下載7K字節 。
在數據存儲,容量計算中,一般又結合公制的進制和二進制的數據計算方法來計算
(二進制)
- 1 byte (B) = 8 bits (b) 字節=8個二進制位
- 1 Kilobyte(K/KB)=210 bytes=1,024 bytes 千字節
- 1 Megabyte(M/MB)=220 bytes=1,048,576 bytes 兆字節
- 1 Gigabyte(G/GB)=230 bytes=1,073,741,824 bytes 千兆字節
- 1 Terabyte(T/TB)=240 bytes=1,099,511,627,776 bytes吉字節
一些存儲器廠家特別是硬盤廠家就更緊密結合十進制來計算,這就是為什么操作系統顯示的容量與廠家標示的容量有些一些差異的原因
(十進制)
- 1 byte (B) = 8 bits (b)
- 1 Kilobyte (K / KB) = 103 bytes = 1,000 bytes
- 1 Megabyte (M / MB) = 106 bytes = 1,000,000 bytes
- 1 Gigabyte (G / GB) = 109 bytes = 1,000,000,000 bytes
- 1 Terabyte (T / TB) = 1012 bytes = 1,000,000,000,000 bytes
翻譯
So to talk, they need to decompose their objects into primitives that the operating system can understand, and marshall the objects across that boundary for you. The code to do that marshalling is tedious to write, so Android handles it for you with AIDL.
盡管如此,進程需要將其對象分解成操作系統能夠識別的原語,并將對象編組成跨越邊界的對象。編寫執行這一編組操作的代碼是一項繁瑣的工作,因此 Android 會使用 AIDL 來處理。
常用代碼
public static String formatTime(long time) {String minute = String.valueOf(time / 60);String second = String.valueOf(time % 60);if (minute.length() == 1) {minute = "0" + minute;}if (second.length() == 1) {second = "0" + second;}return minute + "分" + second + "秒";}DialogFragment
Activity的頁面結構
- PhoneWindow$DecorView
- ActionBarOverlayLayout
- FrameLayout:顯示contentView
- ActionBarcontainer:顯示ActionBar
- ActionBarOverlayLayout
ActionBarOverlayLayout
ActionBarContainer
ActionBarContextView
ProgressBar
android:indeterminateDrawable
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android"android:oneshot="false"><item android:duration="80"><clip android:clipOrientation="horizontal"android:drawable="@drawable/img1"android:gravity="left"/></item><item android:duration="80"><clip android:clipOrientation="horizontal"android:drawable="@drawable/img2"android:gravity="left"/></item><item android:duration="80"><clip android:clipOrientation="horizontal"android:drawable="@drawable/img3"android:gravity="left"/></item><item android:duration="80"><clip android:clipOrientation="horizontal"android:drawable="@drawable/img4"android:gravity="left"/></item><item android:duration="80"><clip android:clipOrientation="horizontal"android:drawable="@drawable/img5"android:gravity="left"/></item><item android:duration="80"><clip android:clipOrientation="horizontal"android:drawable="@drawable/img6"android:gravity="left"/></item><item android:duration="80"><clip android:clipOrientation="horizontal"android:drawable="@drawable/img7"android:gravity="left"/></item><item android:duration="80"><clip android:clipOrientation="horizontal"android:drawable="@drawable/img8"android:gravity="left"/></item><item android:duration="80"><clip android:clipOrientation="horizontal"android:drawable="@drawable/img9"android:gravity="left"/></item><item android:duration="80"><clip android:clipOrientation="horizontal"android:drawable="@drawable/img10"android:gravity="left"/></item><item android:duration="80"><clip android:clipOrientation="horizontal"android:drawable="@drawable/img11"android:gravity="left"/></item><item android:duration="80"><clip android:clipOrientation="horizontal"android:drawable="@drawable/img12"android:gravity="left"/></item><item android:duration="80"><clip android:clipOrientation="horizontal"android:drawable="@drawable/img13"android:gravity="left"/></item><item android:duration="80"><clip android:clipOrientation="horizontal"android:drawable="@drawable/img14"android:gravity="left"/></item><item android:duration="80"><clip android:clipOrientation="horizontal"android:drawable="@drawable/img15"android:gravity="left"/></item><item android:duration="80"><clip android:clipOrientation="horizontal"android:drawable="@drawable/img16"android:gravity="left"/></item><item android:duration="80"><clip android:clipOrientation="horizontal"android:drawable="@drawable/img17"android:gravity="left"/></item> </animation-list>Adapter
public void addAll(List<T> elem) {this.data.addAll(elem);notifyDataSetChanged();}public void remove(T elem) {this.data.remove(elem);notifyDataSetChanged();}public void remove(int index) {this.data.remove(index);notifyDataSetChanged();}public void replaceAll(List<T> elem) {this.data.clear();this.data.addAll(elem);notifyDataSetChanged();} public class CookFoodFragment extends BaseFragment {private boolean hasMore = true;public boolean isFirstLoad = true;private int mCurrentPage = 1;private int mPage = 1;ListView listView;private AddCustomCookAdapter mAdapter;private List<CustomCookItem> mFoodList = new ArrayList();PullToRefreshListView mPullRefreshListView;public int mTimeType = -1;public String record_on;public static CookFoodFragment newInstance(int time_type, String record_on) {CookFoodFragment fragment = new CookFoodFragment();fragment.mTimeType = time_type;fragment.record_on = record_on; return fragment;}public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {return inflater.inflate(R.layout.ft, container, false);}public void onViewCreated(View view, Bundle savedInstanceState) {super.onViewCreated(view, savedInstanceState);ButterKnife.inject((Object) this, view);EventBus.getDefault().register(this);}public void onActivityCreated(Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);this.listView = (ListView) this.mPullRefreshListView.getRefreshableView();this.listView.addHeaderView(LayoutInflater.from(getActivity()).inflate(R.layout.h4, null));this.mAdapter = new AddCustomCookAdapter(getActivity(), this.mFoodList);this.listView.setAdapter(this.mAdapter);this.mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {public void onRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {CookFoodFragment.this.mPage = 1;CookFoodFragment.this.mCurrentPage = CookFoodFragment.this.mPage;CookFoodFragment.this.hasMore = true;CookFoodFragment.this.loadData();}});this.mPullRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {public void onLastItemVisible() {if (CookFoodFragment.this.mPage > CookFoodFragment.this.mCurrentPage) {CookFoodFragment.this.mCurrentPage = CookFoodFragment.this.mPage;CookFoodFragment.this.loadData();}}});this.listView.setOnItemClickListener(new OnItemClickListener() {public void onItemClick(AdapterView<?> parent, View view, int position, long id) {if (!CookFoodFragment.this.isRemoved()) {if (position == 1) {CustomCookListActivity.comeOnBaby(CookFoodFragment.this.getActivity());}CustomCookItem customCook = (CustomCookItem) parent.getAdapter().getItem(position);if (customCook != null) {RecordFood recordFood = new RecordFood();recordFood.time_type = CookFoodFragment.this.mTimeType;recordFood.record_on = CookFoodFragment.this.record_on;recordFood.amount = 1.0f;recordFood.calory = customCook.calory;recordFood.food_name = customCook.name;recordFood.unit_name = "份";if (!TextUtils.isEmpty(customCook.photo)) {if (customCook.photo.startsWith("http")) {recordFood.thumb_img_url = customCook.photo;} else {recordFood.thumb_img_url = TimeLinePatterns.WEB_SCHEME + customCook.photo;}}AddCustomDietFragment.newInstance(0, recordFood).show(CookFoodFragment.this.getFragmentManager(), "addCustomDietFragment");}}}});}public void firstLoad() {new Handler().postDelayed(new Runnable() {public void run() {if (CookFoodFragment.this.isAdded()) {CookFoodFragment.this.mPullRefreshListView.setRefreshing();CookFoodFragment.this.isFirstLoad = false;}}}, 500);}private void loadData() {if (this.hasMore) {FoodApi.getCustomMenus(getActivity(), this.mPage, new JsonCallback(getActivity()) {public void ok(JSONObject object) {super.ok(object);CookFoodFragment.this.refreshData(object);}public void onFinish() {super.onFinish();CookFoodFragment.this.mPullRefreshListView.onRefreshComplete();}});}}private void refreshData(JSONObject object) {if (this.mPage == 1) {this.mFoodList.clear();}List<CustomCookItem> foodList = FastJsonUtils.parseList(object.optString("menus"), CustomCookItem.class);if (foodList == null || foodList.size() <= 0) {this.hasMore = false;} else {this.mFoodList.addAll(foodList);this.mPage++;}this.mAdapter.notifyDataSetChanged();}public void onEventMainThread(MyFoodEvent myFoodEvent) {if (myFoodEvent != null) {switch (myFoodEvent.getFlag()) {case 3:this.mPage = 1;loadData();return;default:return;}}}public void onDestroyView() {super.onDestroyView();ButterKnife.reset(this);EventBus.getDefault().unregister(this);} }虛線
<View android:id="@id/divider_weight"android:layout_width="80.0dip"android:layout_height="3.0px"android:layout_below="@id/tv_weight"android:layout_centerHorizontal="true"android:background="@drawable/bg"android:layerType="software"/> <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="line"><stroke android:width="2.0px"android:color="#44ffffff"android:dashGap="1.0dip"android:dashWidth="2.0dip"/> </shape>background
<?xml version="1.0" encoding="utf-8"?> <shape android:shape="rectangle"xmlns:android="http://schemas.android.com/apk/res/android"><corners android:radius="4.0dip" /><stroke android:width="1.0px" android:color="#ffdfdfdf" /><solid android:color="#ffffffff" /> </shape> <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><corners android:radius="4.0dip"/><stroke android:width="1.0px"android:color="#ff62cc74"/><solid android:color="#ffffffff"/> </shape>Space
總結
以上是生活随笔為你收集整理的Android开发日常笔记的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 处理键盘输入
- 下一篇: androidannotations