我的手機是有劉海的,進入Unity界面後,它直接完全全屏了,導致劉海部分擋住了部分內容(正好我在那裏放了幾個按鈕),需要做一個Header,高度根據劉海自動調整。
一、自動調整標題高度
參考:Android 座標總結
//獲取狀態欄高度
public static float getStatusBarHeight(Context context) {
int height = 0;
try {
Resources resources = context.getResources();
int resourceId = resources.getIdentifier("status_bar_height", "dimen","android");
height = resources.getDimensionPixelSize(resourceId);
} catch (Exception e) {
e.printStackTrace();
}
return height;
}
在Activity的Create裏面設置
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_unity_video_play);
LinearLayout llUnityHeader=findViewById(R.id.llUnityHeader);//Header
ViewGroup.LayoutParams lp=llUnityHeader.getLayoutParams();
lp.height=(int) MeasureUtil.getStatusBarHeight(this);
llUnityHeader.setLayoutParams(lp);
}
二、自動調整字體大小
要用到自動調整字體大小的TextView,因爲我要修改LinnearLayout的高度,要讓裏面的TextView相應的調整字體大小,充滿內容。
參考1:Android自定義TextView根據控件高度和寬度,解決字體適配問題
https://github.com/AndroidDeveloperLB/AutoFitTextView
結論來看,它的例子有效果,但是我不知道怎樣拿來用。
我需要的是界面上把TextView改成AutoTextView後,自動就調整的,不用任何其他代碼的。
github上也找了其他的,居然沒有我要的。
-------------------------------------------------------
參考2:Android 設置TextView自動調整字體大小
結論來說,Android自己的TextView已經支持了,不需要另外弄其他的東西,加個android:autoSizeTextType="uniform"就好了。
從開始到知道花了60分鐘,我現在學過的Android知識只有LinearLayout和RelativeLayout,後面的教程都還沒看。