kotlin開(kāi)發(fā)總結(jié)

目錄

  • 常用數(shù)組的創(chuàng)建
  • Recyclerview中GridLayoutManager間距設(shè)置
  • 設(shè)置自己應(yīng)用為luncher
  • Android 屏幕適配方案
  • Android控件Style
  • 設(shè)置androidstudio自帶模擬器相應(yīng)的dpi
  • 彈框總結(jié)
  • ConstraintLayout
  • 安卓開(kāi)機(jī)自動(dòng)啟動(dòng)app
  • kotlin中重寫(xiě)接口的寫(xiě)法
  • kotlin json轉(zhuǎn)bean

<span id="jump">跳轉(zhuǎn)到的地方</span>

常用數(shù)組的創(chuàng)建:

創(chuàng)建values中的arrays.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    //首頁(yè)條目圖片id
    <string-array  name="home_item_images">
        <item>@drawable/home_item_training</item>
        <item>@drawable/home_item_training</item>
        <item>@drawable/home_item_training</item>
        <item>@drawable/home_item_training</item>
        <item>@drawable/home_item_training</item>
        <item>@drawable/home_item_training</item>
        <item>@drawable/home_item_training</item>
        <item>@drawable/home_item_training</item>
    </string-array>
    //首頁(yè)條目題目
    <string-array name="home_item_titles">
        <item>培訓(xùn)</item>
        <item>人員信息</item>
        <item>運(yùn)營(yíng)圖</item>
        <item>通知</item>
        <item>車(chē)站信息</item>
        <item>圖片文件</item>
        <item>車(chē)站文件</item>
        <item>設(shè)置</item>
    </string-array>
</resources>

//獲取TITLE數(shù)組

var titlesDes: Array<String> = App.mContext.resources.getStringArray(R.array.home_item_titles)
var images = App.mContext.resources.obtainTypedArray(R.array.home_item_images)

創(chuàng)建存儲(chǔ)title和icon的類(lèi)

data class HomeItem(var title: String, var imageResource: Int)

創(chuàng)建一個(gè)集合存儲(chǔ)HomeItem
var mDataList = ArrayList<HomeItem>()
存儲(chǔ)到list中

titlesDes.indices
               .map { HomeItem(titlesDes[it],images.getResourceId(it, 0)) }
               .forEach { mDataList.add(it) }
  • 獲取像素密度
//獲得手機(jī)的寬度和高度像素單位為px
    // 通過(guò)WindowManager獲取
    fun getScreenDensity_ByWindowManager() {
        val mDisplayMetrics = DisplayMetrics()//屏幕分辨率容器
        windowManager.defaultDisplay.getMetrics(mDisplayMetrics)
        val width = mDisplayMetrics.widthPixels
        val height = mDisplayMetrics.heightPixels
        val density = mDisplayMetrics.density
        val densityDpi = mDisplayMetrics.densityDpi
        Log.d("haha", "Screen Ratio: [" + width + "x" + height + "],density=" + density + ",densityDpi=" + densityDpi)
        Log.d("haha", "Screen mDisplayMetrics: " + mDisplayMetrics)
    }
Recyclerview中GridLayoutManager間距設(shè)置

Recyclerview我用的BRVH

//BRVH的引入
compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.22'
//recyclerview引入
compile 'com.android.support:recyclerview-v7:25.3.1'

默認(rèn)是等間分配的,都是等距



給設(shè)置左側(cè)間距變?yōu)?00的時(shí)候就變成上圖那樣了,下面看代碼實(shí)現(xiàn):

  • 先設(shè)置ItemDecoration
homeRecyclerView.addItemDecoration(SpaceItemDecoration(100))
  • 編寫(xiě)SpaceItemDecoration繼承RecyclerView.ItemDecoration
class SpaceItemDecoration(var space: Int) : RecyclerView.ItemDecoration() {

    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        when (parent.getChildAdapterPosition(view) % 4) {
            0 -> outRect.left = 0 //第一列左邊貼邊(默認(rèn)狀態(tài))
            1 -> outRect.left = space//第二列移動(dòng)一個(gè)space
            2 -> outRect.left = space * 2//第三列移動(dòng)二個(gè)space
            3 -> outRect.left = space * 3//第四列移動(dòng)三個(gè)space
        }

        if (parent.getChildAdapterPosition(view) >= 4) {
            outRect.top = dip2px(40f)//檔超過(guò)一排后,剩下的每排距離上面一排增加40高度
        } else {
            outRect.top = 0   //第一排不變
        }
    }
}

知道這些后就好辦了直接按照自己需求設(shè)置就可以了

設(shè)置自己應(yīng)用為luncher

很簡(jiǎn)單只需要在AndroidManifest.xml里的MainActivity<intent-filter>節(jié)點(diǎn)下添加以下代碼:

<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />

添加完了之后的代碼:

<activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
                <category android:name="android.intent.category.HOME" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
</activity>

上面代碼設(shè)置完就可以設(shè)置下默認(rèn)桌面


設(shè)置之后還要屏蔽掉

Android 屏幕適配方案

利用autolayout.jar
Java -jar xx.jar width height width,height_width,height
第一個(gè)為標(biāo)準(zhǔn)圖的寬高 接下來(lái)的像素就是需要添加的像素

Android控件Style

 <!--首頁(yè)上面標(biāo)題樣式-->
    <style name="HomeTitleStyle">
        <item name="android:textSize">28sp</item>
        <item name="android:textColor">#d3d3d3</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_centerInParent">true</item>
    </style>

在需要顯示相同效果的地方運(yùn)用

 <TextView
      style="@style/HomeTitleStyle"
      android:text="@string/home_toolbar_title1" />

設(shè)置androidstudio自帶模擬器相應(yīng)的dpi

在大屏設(shè)備的適配中,往往自帶的模擬器是滿(mǎn)足不了的,例如當(dāng)知道用戶(hù)需要的設(shè)備是160dpi的像素密度,55寸顯示屏,我們不可能時(shí)刻帶著設(shè)備處理問(wèn)題,就需要更改模擬器自帶的Tv模擬器

  • 首先創(chuàng)建一個(gè)1920*1080的模擬器
  • 去用戶(hù)目錄下找到設(shè)備的C:\Users\.android\avd\Android_TV_1080p_API_23.avd
    打開(kāi)其中的cache.img文件,找到其中的hw.lcd.density=160這樣就設(shè)置好了dpi為160了

彈框總結(jié)

自定義Dialog實(shí)現(xiàn)步驟及封裝
Android 封裝一個(gè)通用的PopupWindow
Android PopupWindow詳解

獲取dialog里面的布局控件要在show之后才有效否則為null

mStationDialog.setOnShowListener(object : DialogInterface.OnShowListener {
                override fun onShow(dialog: DialogInterface?) {            
                       val tvZhanQu1 = mStationDialog.findViewById(R.id.tvZhanQu1) as TextView//小箭頭

                }
            })

ConstraintLayout

  • 位于Inspector最中間的那個(gè)正方形區(qū)域,它是用來(lái)控制控件大小的。一共有三種模式可選,每種模式都使用了一種不同的符號(hào)表示,點(diǎn)擊符號(hào)即可進(jìn)行切換。


    表示wrap content,這個(gè)我們很熟悉了,不需要進(jìn)行什么解釋。


    表示固定值,也就是給控件指定了一個(gè)固定的長(zhǎng)度或者寬度值。

    表示any size,它有點(diǎn)類(lèi)似于match parent,但和match parent并不一樣,是屬于ConstraintLayout中特有的一種大小控制方式,any size是用于填充滿(mǎn)當(dāng)前控件的約束規(guī)則,而match——parent是填充整個(gè)父控件

安卓開(kāi)機(jī)自動(dòng)啟動(dòng)app

  • 1 新建一個(gè)類(lèi)繼承BroadcastReceiver, 監(jiān)聽(tīng)系統(tǒng)的BOOT_COMPLETED

/**
 * 檢測(cè)開(kāi)機(jī)啟動(dòng)
 */
class BootBroadcastReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        if (intent.action.equals("android.intent.action.BOOT_COMPLETED")) {
            val mainActivityIntent = Intent(context, MainActivity::class.java)
            mainActivityIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            context.startActivity(mainActivityIntent)
        }
    }
}

2 在配置文件中添加權(quán)限

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

3 在配置文件中注冊(cè)receiver

<receiver android:name=".receiver.BootBroadcastReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
</receiver>

kotlin中重寫(xiě)接口的寫(xiě)法

kotlin中重寫(xiě)接口的時(shí)候要用object來(lái)表達(dá)

mStationDialog.setOnShowListener(object : DialogInterface.OnShowListener {
                override fun onShow(dialog: DialogInterface?) {
                    tvZhanQuShow = mStationDialog.findViewById(R.id.tvZhanQuShow) as TextView
                }
            })

kotlin json轉(zhuǎn)bean

http://www.demojameson.com/2017/05/29/convert-json-to-kotlin-data-class/

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,828評(píng)論 25 709
  • 內(nèi)容抽屜菜單ListViewWebViewSwitchButton按鈕點(diǎn)贊按鈕進(jìn)度條TabLayout圖標(biāo)下拉刷新...
    皇小弟閱讀 47,136評(píng)論 22 665
  • 原文鏈接:https://github.com/opendigg/awesome-github-android-u...
    IM魂影閱讀 33,143評(píng)論 6 472
  • 一棵樹(shù)拂過(guò)秋風(fēng),搖落了葉。 一只貓路過(guò)行人,留下了心。 冷日告別薄霧, 又與漫天陰云昏昏同眠。 小魚(yú)干在掌紋間游走...
    知筆閱讀 390評(píng)論 1 2
  • 早安
    情木子閱讀 135評(píng)論 0 0

友情鏈接更多精彩內(nèi)容