探索Android Design Support Library v28的新控件

Design 28 最近發(fā)布一系列新組件,目前還在alpha版本中疆虚,本文是在Kotlin下針對(duì)這些組件使用的示例苛败,效果圖如下:

Android Design Support Library v28

從上到下依次為:MaterialButton 、Chip径簿、ChipGroup罢屈、MaterialCardView、BottomAppBar

環(huán)境與配置

  1. android studio>= 3.1
  2. sdk platforms = Android P
  3. sdk tools 勾選 Build-tools 28-rc2
  4. 在build.gradle(app)中
android {
    compileSdkVersion "android-P"
   ......
}
dependencies {
    .....
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'
    implementation 'com.android.support:design:28.0.0-alpha1'
}

5.更改styles主題

 <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
        <!-- Customize your theme here. -->
        
    </style>

1.MaterialButton

我們可以將這個(gè)按鈕添加到布局文件中牍帚,如下所示:

 <android.support.design.button.MaterialButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="MaterialButton"
            app:backgroundTint="@color/colorPrimary"
            app:icon="@drawable/ic_adb_white_24dp"
            app:iconTint="#e9a404"
            app:cornerRadius="7dp"
            app:rippleColor="@color/colorAccent"
            />
MaterialButton

屬性說明:

app:icon :定義MaterialButton的圖標(biāo)
app:iconTint :對(duì)圖標(biāo)進(jìn)行染色
app:iconTintMode :定義圖標(biāo)的色調(diào)模式
app:iconPadding :圖標(biāo)內(nèi)邊距
app:additionalPaddingLeftForIcon :圖標(biāo)左邊額外的邊距
app:additionalPaddingRightForIcon? :圖標(biāo)右邊額外的邊距(圖標(biāo)距離文字的距離)
app:rippleColor :按鈕點(diǎn)擊時(shí)的波紋效果顏色
app:backgroundTint? :以避免破壞按鈕樣式儡遮,使用此屬性更改按鈕的背景色
app:backgroundTintMode? :背景色色調(diào)模式
app:strokeColor :描邊的顏色值
app:strokeWidth? :描邊的顏色寬度
app:cornerRadius? :圓角半徑

2. Chip

自帶原型背景的用戶可選擇的建議列表,我們可以像這樣將Chip添加到布局中暗赶,使用app:chipText屬性設(shè)置要顯示在Chip上的文本::

//布局
 <android.support.design.chip.Chip
            android:layout_marginTop="30dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:id="@+id/single_chip"
            app:chipText="A single Chip"
            app:closeIcon="@drawable/ic_close_circle_black_24dp"
            app:closeIconEnabled="true"
            />

//代碼中設(shè)置點(diǎn)擊事件
 single_chip.setOnCloseIconClickListener {
            single_chip.visibility= View.INVISIBLE
        }
Chip

屬性說明:

app:checkable :用于聲明Chip是否可以切換為選定/不選定鄙币。如果禁用肃叶,則檢查的行為方式與按鈕相同。
app:chipIcon? :用于顯示Chip圖標(biāo)
app:closeIcon :顯示關(guān)閉圖標(biāo)(chipIcon?在前十嘿,closeIcon在文字后)
closeIconEnabled :默認(rèn)為true

2.1 ChipGroup

如果我們向用戶顯示一組Chip因惭,我們希望確保它們?cè)谖覀兊囊晥D中正確地組合在一起。如果我們想使用ChipGroup绩衷,我們只需要將我們的芯片視圖封裝在一個(gè)父ChipGroup組件中:

 <android.support.design.chip.ChipGroup
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="30dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <android.support.design.chip.Chip
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:chipText="Chip"
                android:id="@+id/ChipGroupChip1"
                app:chipBackgroundColor="@color/colorAccent"
                app:chipIcon="@drawable/ic_adb_white_24dp"
                app:closeIcon="@drawable/ic_close_circle_black_24dp"
                app:closeIconEnabled="true"
                />
            <android.support.design.chip.Chip
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:chipText="Group"
                />
            <android.support.design.chip.Chip
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:chipText="good"
                />
        </android.support.design.chip.ChipGroup>
ChipGroup

屬性說明:

app:chipSpacing? :水平和垂直軸上chip間距
app:chipSpacingHorizontal :水平間距
app:chipSpacingVertical? :垂直間距
app:singleLine :chip顯示在ChipGroup容器內(nèi)的一行中蹦魔,需要將ChipGroup包裝在滾動(dòng)視圖(如HorizontalScrollView)中,這樣用戶就可以滾動(dòng)顯示芯片咳燕。

3. Material Card View

另一種樣式的CardView組件勿决,在布局中添加:

  <android.support.design.card.MaterialCardView
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="60dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:strokeColor="@color/colorPrimary"
            app:strokeWidth="1dp"
            >
            <TextView
                android:padding="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2018/5/21\nThis is a MaterialCardView "
                android:textAlignment="center"
                />

        </android.support.design.card.MaterialCardView>
MaterialCardView

屬性說明

app:strokeColor :描邊畫筆顏色
app:strokeWidth? :描邊畫筆寬度
app:cardBackgroundColor :背景顏色

4.Bottom App Bar

底部的應(yīng)用程序欄是一個(gè)新的組件,它允許我們?cè)诓季值牡撞匡@示一個(gè)類似工具欄的組件招盲。這允許我們以比標(biāo)準(zhǔn)工具欄更容易與其交互的方式向用戶顯示組件低缩。將BottomAppBar添加到布局文件中,如下所示:

<android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="36dp"
        app:fabSize="normal"
        android:src="@drawable/ic_add_white_24dp"
        app:layout_anchor="@id/bottom_app_bar"
        />
    <android.support.design.bottomappbar.BottomAppBar
        android:id="@+id/bottom_app_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/mtrl_bottomappbar_height"
        android:layout_gravity="bottom"
        app:backgroundTint="@color/colorPrimary"
        app:fabAttached="true"
        app:fabAlignmentMode="end"
        />
BottomAppBar+FloatingActionButton

BottomAppBar配合FloatingActionButton一起使用曹货,屬性說明:

app:layout_anchor:聲明FloatingActionButton固定到BottomAppBar
app:fabAttached? :聲明是否有一個(gè)FAB已被附加到底部的應(yīng)用程序欄咆繁。
app:fabAlignmentMode :FloatingActionButton在BottomAppBar上的位置聲明(center/end)
app:fabCradleVerticalOffset? :標(biāo)記用于FB的垂直偏移量。默認(rèn)情況下0dp
app:backgroundTint? ;BottomAppBar背景色

在Activity代碼中添加菜單

  override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        setSupportActionBar(bottom_app_bar)
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.bottom_app_bar_menu,menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        if (item?.itemId==R.id.action_show_toast){
            Toast.makeText(this,"你好",Toast.LENGTH_LONG).show()
        }
        return true
    }

bottom_app_bar_menu.xml

<?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">
    <item
        android:id="@+id/action_show_toast"
        android:title="Show toast"
        android:icon="@drawable/ic_more_vert_black_24dp"
        app:showAsAction="ifRoom"
        />
    <item
        android:id="@+id/item2"
        android:title="item2"
        android:icon="@drawable/ic_adb_white_24dp"
        app:showAsAction="ifRoom"
        />

</menu>

代碼已打包上傳Csdn下載

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末顶籽,一起剝皮案震驚了整個(gè)濱河市玩般,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌礼饱,老刑警劉巖坏为,帶你破解...
    沈念sama閱讀 216,372評(píng)論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異慨仿,居然都是意外死亡久脯,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門镰吆,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人跑慕,你說我怎么就攤上這事万皿。” “怎么了核行?”我有些...
    開封第一講書人閱讀 162,415評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵牢硅,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我芝雪,道長(zhǎng)减余,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,157評(píng)論 1 292
  • 正文 為了忘掉前任惩系,我火速辦了婚禮位岔,結(jié)果婚禮上如筛,老公的妹妹穿的比我還像新娘。我一直安慰自己抒抬,他們只是感情好杨刨,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,171評(píng)論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著擦剑,像睡著了一般妖胀。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上惠勒,一...
    開封第一講書人閱讀 51,125評(píng)論 1 297
  • 那天赚抡,我揣著相機(jī)與錄音,去河邊找鬼纠屋。 笑死涂臣,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的巾遭。 我是一名探鬼主播肉康,決...
    沈念sama閱讀 40,028評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼灼舍!你這毒婦竟也來了吼和?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,887評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤骑素,失蹤者是張志新(化名)和其女友劉穎炫乓,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體献丑,經(jīng)...
    沈念sama閱讀 45,310評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡末捣,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,533評(píng)論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了创橄。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片箩做。...
    茶點(diǎn)故事閱讀 39,690評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖妥畏,靈堂內(nèi)的尸體忽然破棺而出邦邦,到底是詐尸還是另有隱情,我是刑警寧澤醉蚁,帶...
    沈念sama閱讀 35,411評(píng)論 5 343
  • 正文 年R本政府宣布燃辖,位于F島的核電站,受9級(jí)特大地震影響网棍,放射性物質(zhì)發(fā)生泄漏黔龟。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,004評(píng)論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望氏身。 院中可真熱鬧巍棱,春花似錦、人聲如沸观谦。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽豁状。三九已至捉偏,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間泻红,已是汗流浹背夭禽。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評(píng)論 1 268
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留谊路,地道東北人讹躯。 一個(gè)月前我還...
    沈念sama閱讀 47,693評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像缠劝,于是被迫代替她去往敵國(guó)和親潮梯。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,577評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容