- Toast.LENGTH_SHORT和 Toast.LENGTH_LONG分別對應多長時間?
static final long SHORT_DURATION_TIMEOUT = 4000;
static final long LONG_DURATION_TIMEOUT = 7000;
多次調(diào)用toast.show會依次展示,展示本次view之前會把之前的view remove掉
2.反射可以獲取泛型類型嗎?
getGenericType()
https://www.zhihu.com/question/346911525/answer/830285753
https://cloud.tencent.com/developer/article/1447092
3.hashMap鏈表長度為什么超過8轉化為紅黑樹?
https://blog.csdn.net/reliveIT/article/details/82960063
紅黑樹節(jié)點數(shù)小于6轉換為鏈表
統(tǒng)計計算,柏松分布,時間,空間權衡。
4.rxjava如何實現(xiàn)線程切換?
Scheduler scheduleDirect, 新建線程池,worker隊列
ioThread, computationthread, new Thread
UI MainThread:Handler切換
5.Fragment not attached to activity?
可能發(fā)生在Activity銷毀重建時,fragment中需要context進行布局操作的時候(tvMsg.setText(getResources().getString(R.string.app_name)))。解決如下:
Activity activity = getActivity();
if (isAdded() && activity != null) {
...
}
Fragment保存狀態(tài):setRetainInstance(true);
但onAttach和onActivityCreated還會被重新調(diào)用,只是不調(diào)用onDestroy和onCreate. 設置了setRetainInstance方法,旋轉屏幕時,EditText中輸入的內(nèi)容也會被清除呢?這是因為Fragment的onCreateView方法被重新執(zhí)行了,重新創(chuàng)建了一個新的View,以前輸入的內(nèi)容就沒有了。可以設置一個全局的View,方法如下:
View view = null;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if ( view == null ) {
Log.e("TestFragment", "view == null");
view = inflater.inflate(R.layout.fragment_second, container, false);
}
return view;
}
6.ContentProvider: onCreate調(diào)用時機:Application :attachBaseContext之后,Application: onCreate之前;所以在ContentProvider: onCreate中getContext 可返回application,也可以做一些初始化操作(三方sdk);
- double和float進行浮點計算會產(chǎn)生精度問題,使用 BigDecimal解決,BigDecimal傳遞參數(shù)要使用string,否則也有有問題。
var a = 0.4f
// var b = a*100
(BigDecimal(floatNum.toString()).multiply(BigDecimal(100)))
- attrs和theme可實現(xiàn)不同包使用不同的主題;或者不同flavor的上層依賴同一個sdk,如果該sdk要展示同樣的布局,只是顏色or背景不一致,可以在sdk中將差異化屬性定義為attrs,然后在上層定義theme,在不同的flavor中為屬性設置差異化值。
//attrs.xml 增加屬性的定義
<!-- 按鈕背景色 -->
<attr name="confirm_btn_color" format="reference|color" />
//drawable/positive_btn_bg.xml。使用屬性
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="?attr/confirm_btn_color" />
<corners android:radius="6dp" />
</shape>
//layout.xml 使用背景
<Button
android:id="@+id/confirm_btn"
android:background="@drawable/positive_btn_bg"
.../>
//style.xml;上層項目,在不同flavor包中定義相同的style,為attr賦值
<style name="DIYStyle" >
<item name="confirm_btn_color">@color/color_common_main</item>
</style>
//屬性使用:設置使用Theme
- ViewStub使用
private var mLayout: ViewStub = view.findViewById<ViewStub>(R.id.layout)
val view = mLayout.inflate()
//mLayout .findViewById(R.id.title) 會返回空,要用inflate結果findView才可以
val title: TextView = view.findViewById(R.id.title)