1.問題場景:
新建聯繫人時進行橫屏切換,左側頭像區域底部顯示不全,如下圖所示:
2.佈局分析:
(1)land/contact_editor_fragment.xml源文件:
<com.android.contacts.editor.RawContactEditorView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/raw_contacts_editor_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_primary"
android:orientation="horizontal"
android:visibility="invisible">
<include layout="@layout/photo_editor_view" />
<!-- Dummy view so the first input field is not initially focused. b/21644158 -->
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:focusable="true"
android:focusableInTouchMode="true"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fadingEdge="none"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="@layout/editor_account_header" />
<include layout="@layout/contact_editor_fields" />
</LinearLayout>
</ScrollView>
</com.android.contacts.editor.RawContactEditorView>
其中,photo_editor_view.xml佈局如下:
<com.android.contacts.editor.PhotoEditorView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/photo_editor"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal">
<com.android.contacts.widget.QuickContactImageView
android:id="@+id/photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>
<View
android:id="@+id/photo_icon_overlay"
android:layout_height="56dp"
android:layout_width="match_parent"
android:layout_alignParentBottom="true" />
<ImageView
android:id="@+id/photo_icon"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="22dp"
android:layout_marginEnd="16dp"
android:background="?android:attr/selectableItemBackground"
android:tint="@android:color/white"
android:src="@drawable/quantum_ic_camera_alt_vd_theme_24" />
<View
android:id="@+id/photo_touch_intercept_overlay"
android:focusable="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/item_background_material_dark"
android:contentDescription="@string/editor_change_photo_content_description" />
</com.android.contacts.editor.PhotoEditorView>
(2)分析:
這是一個呈左右方向分佈的佈局,左邊爲頭像部分是一個繼承自相對佈局的自定義view,右邊爲ScrollView展示個人信息列表,乍一看並沒有啥問題。
3.代碼分析:
首先,我的第一個想法是,將左邊的view用scrollview包裹,然後也使用android:fillViewport="true"屬性使scrollview內容充滿屏幕;重新編譯後,安裝到平板上並沒有達到理想的效果,左邊部分變得可以滾動,並且頭像被拉長,只有上下拖動才能完整顯示。
然後,我發現,在豎屏時如果沒有彈出軟鍵盤,在翻轉到橫屏時就不會出現頭像顯示不全的問題。因此,我在onConfigurationChanged 方法中隱藏了鍵盤,可惜還是徒勞。
最後,我研究了scrollview和activity的 android:windowSoftInputMode屬性,發現軟件盤彈起時會佔用activity的主窗口空間,影響與用戶的交互,網上有些博客建議在最外層包裹ScrollView,我試了之後沒有起到作用,考慮到ScrollView也是繼承自FrameLayout,因此我使用FrameLayout對左邊的佈局做了一層包裝,最後測試Ok,根本原因還有待繼續深究,歡迎大家一起探討。
4.問題解決:
針對land/contact_editor_fragment.xml源文件左邊佈局,外面包裹了一層FrameLayout,具體如下所示:
<com.android.contacts.editor.RawContactEditorView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/raw_contacts_editor_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_primary"
android:orientation="horizontal"
android:visibility="invisible">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<include layout="@layout/photo_editor_view" />
</FrameLayout>
<!-- Dummy view so the first input field is not initially focused. b/21644158 -->
<!--<View
android:layout_width="0dp"
android:layout_height="0dp"
android:focusable="true"
android:focusableInTouchMode="true"/>-->
<ScrollView
android:fillViewport="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="@layout/editor_account_header" />
<include layout="@layout/contact_editor_fields" />
</LinearLayout>
</ScrollView>
</com.android.contacts.editor.RawContactEditorView>