2016-11-28 53 views
1

在基本活動中,我試圖將圖像附加到工具欄。在ImageView部分android:layout_height="217dp"中的這段代碼中。我不想這樣說,因爲我最近讀到的算法適用於所有設備。所以我最終的選擇是使用:match_parentwrap_content。如果我使用217dp,我的應用程序將看起來不同。但是,如果我使用match_parentwrap_content作爲圖像視圖,我會得到一個巨大的工具欄,它佔用了幾乎一半的屏幕。我該怎麼辦?工具欄中的圖像:佈局:高度

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

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="217dp" 
      app:srcCompat="@drawable/wagner" 
      android:layout_gravity="left" 
      android:id="@+id/imageView" /> 

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

    <include layout="@layout/content_first" 
     android:layout_height= "wrap_content" /> 

enter image description here

+1

你能提供一個你想要的工具欄的圖像嗎? – lmiguelvargasf

+0

@Imiguelvargasf - 我添加了一張圖片。不要介意DOT ......它在圖像本身。 –

+0

我同意@RahulAgrawal,你可以減少圖像的大小,並應用''wrap_content'',或者你可以像我在我提供的答案中一樣嘗試使用百分比。 – lmiguelvargasf

回答

0

這是因爲圖像的大小發生。您需要通過一些工具來縮小圖像的大小。過去我也有類似的問題。在將圖像保存爲android之前,請嘗試設置圖像的固定高度。它應該解決您的問題

0

在這種情況下,你可以在你的.java文件中使用下面的代碼

getSupportActionBar().setLogo(R.drawable.ic_launcher); 
0

如果你嘗試使用百分比:

<ImageView 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="1" 
    app:srcCompat="@drawable/wagner" 
    android:layout_gravity="left" 
    android:id="@+id/imageView" /> 


<Space 
    android:layout_weight="1" 
    android:layout_width="0dp" 
    android:layout_height="match_parent"/> 

你可以改變號碼layout_weight,調整百分比。在這種情況下,按鈕的每個元素的百分比是50%,並且是空的空間。