歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 簡單FadingActionBar的使用

簡單FadingActionBar的使用

日期:2017/3/1 9:29:10   编辑:Linux編程

這個庫對於現如今使用的Compat版本的ActionBar和Toolbar支持根本沒有,所以建議不要再學習如何使用此庫。

地址:https://github.com/ManuelPeinado/FadingActionBar

上面的結果得出坑了我半天的時間,下面的結論的得出耗費了我另外2個小時的時間。

1.首先,不要在AndroidStudio中使用v7的ActionBarActivity來作為parent,否則會出現NullPointer的異常。

2.使用繼承自Activity的暫時無法支持透明效果,因為如果使用的style的theme使用了v7的AppCompat照樣是空指針(總之,一點跟ActionBar兼容的我這都不好用)

3.不要使用Holo中的DarkActionBar和NoActionBar這兩個theme,一個會顯示白色ActionBar(而且字體也是白色,發虛),一個照樣空指針(這個是手殘試了一下之後的結論)

4.還是建議找一下別的庫(我會繼續尋找的,如果沒有好的實現,我將來可能自己實現一個簡版的)

如何引用庫就不多說了,github上有,支持gradle文件的依賴

使用方式:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

FadingActionBarHelper helper = new FadingActionBarHelper()
.actionBarBackground(R.drawable.ab_background)
.headerLayout(R.layout.header)
.contentLayout(R.layout.activity_listview);
setContentView(helper.createView(this));
helper.initActionBar(this);

ListView listView = (ListView) findViewById(android.R.id.list);
ArrayList<String> items = loadItems(R.raw.nyc_sites);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, items);
listView.setAdapter(adapter);
}

其中涉及了三個部分

actionbarBackground,下拉之後的背景(ActionBar文檔推薦對於ActionBar的背景使用9patch圖像)

headerLayout,Demo中下方圖像顯示區域的布局,這個區域的大小必須雅閣控制,如果與下方ListView出現大面積空白,那麼可能就是你圖像的布局和其他布局被引用後占據的是非預期大小的布局,可以在預覽中看到。

contentLayout就是ListView等布局的位置,下面重點介紹這裡的問題:

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

這裡的問題就是,listView 的 id必須是android開頭的,具體用什麼命名參照demo中的相關用法即可。

模仿效果如下(暫時無法添加ActionBar初始的透明效果)

Copyright © Linux教程網 All Rights Reserved