2015-10-15 102 views
0

我的應用最初是爲Android 2.3開發的,但已更新很多次,目前的目標是V21。所以當我的Nexus 5收到Marshmallow更新並發現我的應用程序的一個重要功能被破壞時,我感到很驚訝。我有一個屏幕,頂部有一行,我想在最右邊的兩個按鈕和一個EditText框(用於搜索文本)將屏幕的其餘部分填充到左側。這是我的第一個應用程序,我不清楚有關各種佈局技術,但在Android 2.3到5.1這個代碼繼續工作:安卓棉花糖破舊佈局

<RelativeLayout android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    style="@style/upper_detail_section" 
    android:elevation="@dimen/upper_elevation" 
> 
    <Button android:id="@+id/sort" 
     style="@style/large_cust_btn" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:text="@string/sort" 
    /> 
    <Button android:id="@+id/clear" 
     style="@style/large_cust_btn" 
     android:layout_toLeftOf="@+id/sort" 
     android:layout_alignParentTop="true" 
     android:layout_alignWithParentIfMissing="true" 
     android:text="@string/clear" 
    /> 
    <EditText android:id="@+id/lookup" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_toLeftOf="@+id/clear" 
     android:layout_alignBaseline="@+id/clear" 
     android:singleLine="true" 
     android:hint="@string/item_search_hint" 
     android:scrollHorizontally="true" 
     android:textSize="16sp" 
     android:inputType="textFilter" 
    /> 
</RelativeLayout> 

將這一畫面起來的Android 6.0,按鍵都存在,但在的EditText場完全消失!知道什麼我現在知道它原來是使用的LinearLayout和加權一個簡單的解決辦法:

<LinearLayout android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    style="@style/upper_detail_section" 
    android:orientation="horizontal" 
    android:elevation="@dimen/upper_elevation" 
    > 
    <EditText android:id="@+id/lookup" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:singleLine="true" 
     android:hint="@string/item_search_hint" 
     android:scrollHorizontally="true" 
     android:textSize="16sp" 
     android:inputType="textFilter" 
     /> 
    <Button android:id="@+id/clear" 
     style="@style/large_cust_btn" 
     android:text="@string/clear" 
     android:onClick="clearClicked" 
     /> 
    <Button android:id="@+id/sort" 
     style="@style/large_cust_btn" 
     android:text="@string/sort" 
     android:onClick="sortClicked" 
     /> 
</LinearLayout> 

所以,一切都很好,現在我更新了新的APK商店,但我想我會警告現在有些佈局技巧的人現在可能會被打破。

+1

這將作爲一個問題和答案更好地處理,以適應網站結構(https://stackoverflow.com/help/self-answer)。如果你這樣做,你也可以考慮在截圖之前和之後,因爲很難理解在這種情況下「破碎」的真正含義。 – CommonsWare

+1

我假設'large_cust_btn'定義了layout_width和layout_height? – njzk2

+0

對不起,我忘了那個細節。 「破碎」我的意思是EditText字段沒有出現在屏幕上。按鈕按預期顯示,但左側只有空白。此外,large_cust_button樣式定義了寬度和高度(例如,帶有一些邊距的wrap_content)。 – gordonwd

回答

0

如果我正確理解並重現您的問題,那麼使用RelativeLayout修復問題也很簡單。棉花糖只能以不同的方式處理寬度match_parent。因此,只要將它設置爲別的東西一樣與LinearLayout和alignParentLeft到true

<EditText android:id="@+id/lookup" 
    android:layout_alignParentLeft="true" 
    android:layout_width="0dp" 
    [...] /> 

這得到了我同樣的佈局像前棉花糖。