關(guān)于知乎新聞案例的模仿

1.ToolBar

參看該文章http://www.itdecent.cn/p/79604c3ddcae
項(xiàng)目參看: SystemUITest

2.RecyclerView

http://www.itdecent.cn/p/f592f3715ae2

  • Android中的自定義屬性
    http://blog.csdn.net/lmj623565791/article/details/45022631
    其中有點(diǎn)問題的是:就我寫的例子而言
    關(guān)于引用自定義控件的命名空間,是放在自定義控件下的(沒問題),而不是放在布局根節(jié)點(diǎn)下的。代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <com.example.systemuitest.AttrsTypedArray.CustomView
       //放在這個(gè)地方?jīng)]有問題
        xmlns:xyz="http://schemas.android.com/apk/res/com.example.systemuitest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        xyz:text="hello"
        xyz:textAttr="29"
        />

</LinearLayout>

關(guān)于命名空間參考:http://blog.csdn.net/janice0529/article/details/34425549

TypedArray是(存放attrs列出的屬性值)方便直接取出

public class CustomView extends View {
    private static final String TAG=CustomView.class.getSimpleName();
    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //Return a TypedArray holding the attribute values in set that are listed in attrs.
        //返回一個(gè)TypedArray(存放attrs列出的屬性值)
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.test);
        String text = ta.getString(R.styleable.test_text);//別忘了是放在styleable之下的
        int textAttr = ta.getInteger(R.styleable.test_textAttr, -1);
        Log.e(TAG, text+"----"+textAttr );

        ta.recycle();
    }
}

關(guān)于第一篇博文中用數(shù)組存儲(chǔ)attr的屬性值,并用TypedArray通過下標(biāo)獲取對應(yīng)的屬性值
會(huì)出現(xiàn)一個(gè)bug:

Error: Expected resource of type styleable

解決方法如下:在使用 TypedArray 的方法處
加上 @SuppressWarnings("ResourceType")

 @SuppressWarnings("ResourceType")

案例代碼

//在這兒加的
@SuppressWarnings("ResourceType")
public class CustomVewActivity extends View {
    private static final String TAG = CustomVewActivity.class.getSimpleName();
    private static final int[] mAttrs = {R.attr.test3, R.attr.test4};

    public CustomVewActivity(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray ta = context.obtainStyledAttributes(attrs, mAttrs);
        String test3 = ta.getString(0);
        int test4 = ta.getInteger(1, -1);

        Log.e(TAG, test3+"--"+test4 );
        ta.recycle();
    }
}


getItemOffsets():從字面意思就是Item要偏移, 由于我們在Item和Item之間加入了分隔線,線其實(shí)本質(zhì)就是一個(gè)長方形,也是用戶自定義的,既然線也有長寬高,就畫橫線來說,上面的Item加入了分隔線,那下面的Item就要往下平移,平移的量就是分隔線的高度。

  • 關(guān)于分割線后續(xù)實(shí)際代碼

上面兩篇已經(jīng)講得很清楚了,原理就是第一篇博文寫的那樣,很清晰明白,需要注意的問題是在列表方向是垂直的時(shí)候(一般為默認(rèn)),需要畫水平線,第一篇博文思路很清晰,問題在于列表方向是垂直的時(shí)候,item應(yīng)該向下平移

3. DrawingCache

緩存視圖.png

關(guān)于這段代碼其中用到DrawingCache,官網(wǎng)API解釋:

void setDrawingCacheEnabled (boolean enabled)

Enables or disables the drawing cache. When the drawing cache is enabled, the next call to getDrawingCache() or buildDrawingCache() will draw the view in a bitmap. Calling [draw(android.graphics.Canvas)]
will not draw from the cache when the cache is enabled. To benefit from the cache, you must request the drawing cache by calling getDrawingCache()
and draw it on screen if the returned bitmap is not null.

關(guān)于參數(shù):
boolean enabled: true to enable the drawing cache, false otherwise

http://1137907860.blog.51cto.com/10452906/1682078中說到

若想更新cache, 必須要調(diào)用destoryDrawingCache方法把舊的cache銷毀,才能建立新的。
>當(dāng)調(diào)用setDrawingCacheEnabled方法設(shè)置為false, 系統(tǒng)也會(huì)自動(dòng)把原來的cache銷毀。

對比看
http://souly.cn/%E6%8A%80%E6%9C%AF%E5%8D%9A%E6%96%87/2016/01/05/DrawingCache%E8%A7%A3%E6%9E%90/

有點(diǎn)不同暫時(shí)先記錄吧

4.ObjectAnimation

關(guān)于這部分之前已經(jīng)接觸過而且比較簡單,只是實(shí)現(xiàn)一下動(dòng)畫的類型直接參看下面一篇文章即可
http://blog.csdn.net/dingfengnupt88/article/details/51556597

透明度(alpha)

透明度.png

代碼實(shí)現(xiàn)如上:該示例代碼并沒有在onAnimationEnd中實(shí)現(xiàn)相應(yīng)函數(shù)

5.ViewStub

參看
http://souly.cn/%E6%8A%80%E6%9C%AF%E5%8D%9A%E6%96%87/2015/09/18/ViewStub%E7%94%A8%E6%B3%95%E5%88%86%E6%9E%90/

1.用途:
最大的用途就是實(shí)現(xiàn)View的延遲加載,在需要使用的時(shí)候再加載view
在這個(gè)項(xiàng)目中作者是用來顯示網(wǎng)絡(luò)通知的(平時(shí)不需要顯示,在無網(wǎng)絡(luò)時(shí)使view可見)
2.特點(diǎn)
ViewStub inflate之后就會(huì)變成空的(所以點(diǎn)擊第一次按鈕textView會(huì)顯示出來,第二次點(diǎn)擊會(huì)報(bào)空指針異常 最好加一個(gè)非空判斷

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

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

  • 曾記得遠(yuǎn)在他鄉(xiāng) 思緒飄渺 小時(shí)候的記憶 如詩般美好 當(dāng)秋風(fēng)起 楓葉落 一地思念 這一時(shí)的景 如癡如醉 如夢似幻 恍...
    sujing123閱讀 197評論 4 5
  • 當(dāng)下網(wǎng)絡(luò)發(fā)達(dá)的時(shí)代,成就了一批批的商業(yè)精英,以馬云為首在中國商業(yè)精英開辟的網(wǎng)上購物熱潮,徹底打破了人們以前的生活和...
    喜水閱讀 790評論 1 3
  • 首先的首先,官網(wǎng)介紹使用和注意事項(xiàng)的地址,說實(shí)話,太長了我自己就看了一點(diǎn)點(diǎn)。 那就說我是如何瞎用的。首先,由于知識(shí)...
    美男子_杰琳塔閱讀 720評論 0 1
  • 完全走出別人眼光的枷鎖,是需要發(fā)展出兩種基本能力: 欣賞能力,能夠欣賞自己和欣賞他人的長項(xiàng); 拒絕能力,能夠否認(rèn)別...
    心理咨詢師Lily閱讀 834評論 0 1

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