2016-09-16 68 views
0

我遇到了一個問題,我瀏覽了整個互聯網尋找解決方案,但沒有任何工作適合我。 我已經在我的工具欄中添加了一個imageview,但它略微向右。它沒有集中對齊,而是向右移動了一點。當我將圖像視圖對齊到左側時,Imageview和工具欄左側之間存在未知的填充/間隙。以下是我的XML代碼:Android - 工具欄的子佈局左側的填充/空白鍵

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <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="match_parent" 
     android:fitsSystemWindows="true" 
     tools:context=".NavigationDrawerHomeScreen.BaseActivity"> 

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

      <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:app="http://schemas.android.com/apk/res-auto" 
       android:id="@+id/toolbar_home_activity" 
       android:layout_width="match_parent" 
       android:layout_height="45dp" 
       android:contentInsetLeft="0dp" 
       android:contentInsetStart="0dp" 
       android:contentInsetStartWithNavigation="0dp" 
       android:minHeight="?attr/actionBarSize" 
       app:contentInsetLeft="0dp" 
       app:contentInsetStart="0dp" 
       app:contentInsetStartWithNavigation="0dp"> 

       <RelativeLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 

        <ImageView 
         android:id="@+id/logo_name" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_centerInParent="true" 
         android:src="@drawable/fixer_title_thick" /> 
       </RelativeLayout> 

       <android.support.v7.widget.SearchView 
        android:id="@+id/search_home" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignParentRight="true" 
        android:showAsAction="ifRoom" 
        android:visibility="gone"> 

       </android.support.v7.widget.SearchView> 


      </android.support.v7.widget.Toolbar> 

     </android.support.design.widget.AppBarLayout> 

     <FrameLayout 

      android:id="@+id/container_layout" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

      <ImageView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:src="@drawable/home_bg" /> 

     </FrameLayout> 

     <!--<TextView--> 
     <!--android:layout_width="wrap_content"--> 
     <!--android:layout_height="wrap_content"--> 
     <!--android:layout_centerInParent="true"--> 
     <!--android:text="this is base" />--> 

    </android.support.design.widget.CoordinatorLayout> 
</RelativeLayout> 

<android.support.design.widget.NavigationView 
    android:id="@+id/nav_view" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:background="#2A2929" 
    android:backgroundTint="#2A2929" 
    android:fitsSystemWindows="true" 
    app:headerLayout="@layout/nav_header_home_customer" 
    app:itemTextAppearance="@style/NavDrawerTextStyle" 
    app:itemTextColor="#fff" 
    app:menu="@menu/activity_home_customer_drawer"> 

    <LinearLayout 
     android:id="@+id/footer" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_gravity="bottom" 
     android:clickable="true" 
     android:orientation="vertical"> 

     <!-- any addition stuff you want in yoour footer layout --> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:layout_marginTop="50dp" 
      android:orientation="vertical"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Set Search Radius" 
       android:textColor="@color/white" /> 

      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="20dp" 
       android:clipChildren="false"> 

       <com.crystal.crystalrangeseekbar.widgets.CrystalSeekbar 
        android:id="@+id/rangeSeekbar1" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 

        app:bar_color="@color/blue" 
        app:left_thumb_color="@color/blue_dark" /> 

       <TextView 
        android:id="@+id/textMin1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_below="@id/rangeSeekbar1" 
        android:layout_marginBottom="10dp" 
        android:layout_marginLeft="10dp" 
        android:layout_marginStart="10dp" 
        android:layout_marginTop="5dp" 
        android:text="0" 
        android:textColor="@color/white" 
        android:textSize="16dp" /> 

       <TextView 
        android:id="@+id/textMax1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentRight="true" 
        android:layout_below="@id/rangeSeekbar1" 
        android:layout_marginEnd="10dp" 
        android:layout_marginRight="10dp" 
        android:layout_marginTop="5dp" 
        android:text="100" 
        android:textColor="@color/white" 
        android:textSize="16dp" /> 

      </RelativeLayout> 
     </LinearLayout> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="48dp" 
      android:background="#1E88E5"> 

      <TextView 
       android:id="@+id/logout_footer" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerVertical="true" 
       android:layout_marginBottom="20dp" 
       android:layout_marginLeft="16dp" 
       android:layout_marginRight="16dp" 
       android:drawableLeft="@drawable/ic_logout_white_24dp" 
       android:drawablePadding="32dp" 
       android:text="Logout" 
       android:textColor="#fff" 
       android:textSize="18sp" /> 
     </RelativeLayout> 
    </LinearLayout> 

</android.support.design.widget.NavigationView> 

的應用:contentinset /安卓:contentinset解決方案適用於一些人,但它不爲我工作。

這裏的圖片http://imgur.com/a/Vp2g6

+0

[Android工具欄中的漢堡(菜單)圖標後刪除大填充可能的重複?](http://stackoverflow.com/questions/38373481/remove-large-padding-after-hamburger-menu-icon-in- android-toolbar) –

+0

@MikeM。我嘗試了所有這些解決方案,但都沒有爲我工作 – Tayyaba

+0

爲了確保我們談論的是同樣的事情,您應該發佈問題圖片和更新後的代碼/ XML。此外,您應該在對佈局進行如此小的更改後清理並重建項目。 –

回答

2

我有這個同樣的問題,我嘗試了很多事情,如設置

app:contentInsetStart ="0dp" ; 

app:contentInsetStartWithNavigation = "0dp" ; 

它沒有工作。我甚至將它們設置爲負值,但它不起作用。我試圖給我的子視圖添加一個右邊距,但它減少了子視圖的可見大小,而不是佔用左邊的空間。 因此,在這個空間中可能不允許子視圖。

所以花了一些時間後,我覺得有2種方法可以解決這個問題,其中一種是我不喜歡的其他方法。首先讓我告訴你一個我不喜歡:

向上按鈕的定製實施:這個問題,只有當出現在您的Java類此佈局設置此工具欄爲你的動作條,你

setDisplayHomeAsUpEnabled(true); 

沒有這個命令這個問題不存在。 因此,如果您不使用默認的後退按鈕並通過放置imageView並提供onClick實現來實現自己的後退按鈕,則應該可以正常工作。

使ChildViews爲兄弟姐妹:另一種方法是使用工具欄的屬性,它只是另一個視圖。 我更喜歡這種方法,因爲通過使用此方法,您可以使用android的默認向上按鈕。您需要做的唯一事情是 - 不是在工具欄內定義子視圖,而是將子視圖和工具欄作爲線性或相對佈局中的同級。這樣,您可以將左側空間僅縮小爲工具欄的「wrap_content」,並利用其他子視圖中的左側空間。在我的應用程序中,我甚至可以在工具欄上使用onClick功能,現在可以通過在兩個佈局的父級上賦予該功能(即在LinearLayout上設置onClick)來完成該功能。

雖然我仍然不知道左側空間存在的確切原因,或者有辦法禁用它,但是這是一個非常乾淨的方法來解決這個問題。