2012-05-22 53 views
23

我正在繪製一個圖層列表。 我有我的背景圖片,我想第二層要小一些, 但似乎不是不管我怎麼樣我的Android裏面寫着:layer_width \高度 第二層尺寸基本保持不變。android如何更改<item>寬度和高度

這是我的xml:

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" > 

<item android:drawable="@drawable/picuser" 
android:layout_width="50dp" 
android:layout_height="50dp"/> 

<item android:drawable="@drawable/ic_launcher" 
android:layout_width="10dp" 
android:layout_height="10dp"/> 

</layer-list> 

回答

16

我希望這把你在正確的方向:

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 

<item android:drawable="@drawable/picuser"/> 

<item> 
     <bitmap android:src="@drawable/ic_launcher" 
     android:gravity="center" /> 
    </item> 

</layer-list> 

正如你可以看到here<item>沒有寬/高屬性。 ..

+2

是的,我已經看到它,但沒有一種方法來定義每個圖層的大小?它真的很重要.. –

+7

由於API 23,有屬性寬度,高度,重力項目 – Pauland

+11

請給解決方案,不只是說不。 – User9527

25

這是怎樣的一個workaounrd的,但它的工作對我來說,你可以使用填充,它將使第二繪製小

<item android:drawable="@drawable/circyle"/> 

<item android:drawable="@drawable/plus" 
     android:top="10dp" android:bottom="10dp" 
     android:right="10dp" android:left="10dp"/> 

2

我嘗試了很多,還沒有找到一種很好的方法來通過xml佈局設置徽標中心。 Finnally我找到了一個解決方法如下:

mToolbar.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
     @Override 
     public void onGlobalLayout() { 
      ImageView imageView = (ImageView)mToolbar.findViewById(R.id.logo); 
      if(mToolbar == null){ 
       return; 
      } 
      int toolbarWidth = mToolbar.getWidth(); 
      int imageWidth = imageView.getWidth(); 
      imageView.setX((toolbarWidth-imageWidth)/2); 
     } 
    }); 

layout_toolbar.xml:

<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" 
     style="@style/Toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="@dimen/toolbar_height"                 
     android:background="@drawable/toolbar_background" 
     android:focusable="false"         
     app:titleTextAppearance="@style/AppTheme.Toolbar.Title"> 
     <ImageView 
      android:id="@+id/logo" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/toolbar_logo"/> 
    </android.support.v7.widget.Toolbar> 
4

從API等級23及更高版本,實際上你可以設置寬度&高度上的項目

<item 
    android:drawable="@drawable/splash_logo" 
    android:height="100dp" 
    android:width="100dp"/> 

注意:這是android:width而不是android:layout_width

1

真的<item>已得到widthheight屬性,代替layout_widthlayout_height屬性,與不可能性的唯一差異將它們設置爲match_parentwrap_content,只是因爲API 23
偏偏這些屬性已經出臺無論如何,如果要使用它們,最終可以使用屬性gravity來實現兒童的match_parent,將其設置爲left|righttop|bottom。 但是因爲這些屬性對於任何實際應用太新,我建議使用此解決方法:

<layer-list> 

    <item> 

     <shape 
      android:shape="rectangle"> 

      <size 
       android:width="152dp" 
       android:height="152dp"/> 

      <solid 
       android:color="#00FFFFFF"/> 
     </shape> 
    </item> 

    <item> 

     <bitmap 
      android:gravity="left|right|top|bottom" 
      android:src="@mipmap/ic_launcher"/> 
    </item> 
</layer-list> 

現在,開始預先假定一個<bitmap>可以得到填補在父母與我上面使用屬性gravity建議的伎倆,和會心一<shape>可以有一個<size>孩子來指定使用widthheight屬性不限制到更新的API它的大小,你可以使用一個<layer-list>創建一個包含一個大小<shape>(透明<solid>),另一個<item><item>可拉伸性未分類的<bitmap>gravity屬性設置爲「left | top | bottom |」,完全填充父級<layer-list>,其大小爲<item>

所有我提到的屬性,當然包含在android:命名空間。

相關問題