2017-01-09 207 views
0

我正在爲搜索活動構建自定義工具欄,它適用於某些設備,不適用於其他屏幕。 我試過在我的設備720 * 1280,它很好。但是當我在genymotion設備768 * 1280上打開應用程序時,它似乎就是這樣。 enter image description here自定義工具欄設計

search_toolbar.xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:fitsSystemWindows="true"> 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/search_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="@drawable/search_bar_bg" 
     app:popupTheme="@style/AppTheme.PopupOverlay"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="horizontal"> 

      <ImageButton 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_marginBottom="2dp" 
       android:layout_weight=".1" 
       android:background="#fff" 
       android:clickable="true" 
       android:onClick="back" 
       android:src="@drawable/ic_arrow_back_black" /> 

      <EditText 
       android:id="@+id/searchText" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_gravity="center_vertical" 
       android:layout_marginLeft="5dp" 
       android:layout_weight=".99" 
       android:hint="@string/search" 
       android:padding="3dp" 
       android:textColor="#000" 
       android:textColorHint="#FFCEC9C9" 
       android:textSize="15sp" 
       android:textStyle="bold" /> 

     </LinearLayout> 
    </android.support.v7.widget.Toolbar> 
</android.support.design.widget.AppBarLayout> 
</android.support.design.widget.CoordinatorLayout> 

Search.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 

<include 
    layout="@layout/search_page_toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

<android.support.v7.widget.RecyclerView 
    android:id="@+id/search_recycler" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="8dp" /> 
</LinearLayout> 

回答

1

你應該修改你Search.xml像這樣(請記住,在你的recyclerview添加app:layout_behavior="@string/appbar_scrolling_view_behavior"):

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:fitsSystemWindows="true"> 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/search_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="@drawable/search_bar_bg" 
     app:popupTheme="@style/AppTheme.PopupOverlay"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="horizontal"> 

      <ImageButton 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_marginBottom="2dp" 
       android:layout_weight=".1" 
       android:background="#fff" 
       android:clickable="true" 
       android:onClick="back" 
       android:src="@drawable/ic_arrow_back_black" /> 

      <EditText 
       android:id="@+id/searchText" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_gravity="center_vertical" 
       android:layout_marginLeft="5dp" 
       android:layout_weight=".99" 
       android:hint="@string/search" 
       android:padding="3dp" 
       android:textColor="#000" 
       android:textColorHint="#FFCEC9C9" 
       android:textSize="15sp" 
       android:textStyle="bold" /> 

     </LinearLayout> 
    </android.support.v7.widget.Toolbar> 
</android.support.design.widget.AppBarLayout> 

<android.support.v7.widget.RecyclerView 
    android:id="@+id/search_recycler" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="8dp" 
app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 

</android.support.design.widget.CoordinatorLayout> 
+0

謝謝,工作正常。但爲什麼我不能使用包括?? – khaledsamer

+0

如果你想包含'search_toolbar.xml'。你只需要把工具欄放到'search_toolbar.xml'中,而不是整個'CoordinatorLayout'。 –

+0

aha。非常感謝兄弟現在 – khaledsamer

0

嘗試在AppBarLayoutandroid:theme="@style/ThemeOverlay.AppCompat.ActionBar"

+0

不是工作壓力太大 – khaledsamer

+0

使用此https://guides.codepath.com/android/Using-the-App-工具欄。希望這是你的解決方案。 –