2012-05-22 7 views
23

私はdrawableのためのレイヤーリストを作っています。 は、私は私の背景イメージを持っている、と私は第二層は、 小さくなりたいが、私が私のアンドロイドの内側に書いているかは重要ではないようです..第二層のサイズは同じままlayer_widthの\高 androidどのように変更する<item>幅と高さ

あなたはhere<item>は、幅/高さ属性を持っていない見ることができるように

<?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> 

:私は、これは正しい方向にあなたを置く願ってい

<?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です。 ..私は多くのことを試してみました

+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:APIレベル23と高いから

<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

は、あなたが実際にアイテムの上に幅&高さを設定することができます

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

注:android:layout_widthの代わりにandroid:widthの代わりに

1

本当に<item>match_parentまたはwrap_contentにそれらを設定するために、不可能のユニークな違いで、layout_widthlayout_height属性の代わりに、widthheight属性を持っているが、不幸それらの属性は、とにかくAPI 23
以来、ちょうど導入されています、それらを使用する場合は、最終的に属性gravityを使用して、子のmatch_parentを実現します。left|rightまたはtop|bottomに設定します。 しかし、これらの属性は、実際のアプリのためにあまりにも新しいなので、私はこの回避策を使用することをお勧め:、<bitmap>私は属性gravityを使用して、上記提案トリックで親に満たされ得ることができることを前提始め、今

<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> 

をそして<shape>が新しいAPIに限らずwidthheightの属性を使用して、そのサイズを指定する<size>子供を持つことができ、あなたが(透明<solid>付き)サイズ<shape><item>と別の<item>とを含む描画可能を作成するために<layer-list>を使用することができます知っていますサイズが指定されていない属性が「left | right | top | bottom」に設定されたは、完全に<layer-list>の親を埋め込みます。サイズは<item>です。

私が言及したすべての属性は、当然のことながら、android:名前空間に含まれています。

関連する問題