2015-10-15 105 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" 
    [...] /> 

这得到了我同样的布局像前棉花糖。