2012-01-12 6 views
10

私はView背景として使用しているタイルビットマップを持っています。このViewは、android:layout_height="wrap_content"です。問題は、バックグラウンドで使用されているビットマップの高さがビューの測定に関与しており、高さがViewであることです。これは、コンテンツのサイズがViewであるときに、タイルの背景として使用されるビットマップの高さよりも小さいことに気付くことができる。タイル張りの背景がそれを押しています。表示サイズ

例を示しましょう。タイルビットマップ:

enter image description here

ビットマップ描画可能(tile_bg.xml):

<?xml version="1.0" encoding="utf-8"?> 
<bitmap xmlns:android="http://schemas.android.com/apk/res/android" 
    android:src="@drawable/tile" 
    android:tileMode="repeat"/> 

レイアウト:

それがどのように見えるか
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:background="#FFFFFF"> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/tile_bg" 
     android:text="@string/hello" 
     android:textColor="#000000" /> 

</LinearLayout> 

enter image description here

TextViewの高さは、ビットマップの高さになります。私が期待していたのは、ビットマップがViewのサイズにクリップされるということです。

これを達成する方法はありますか?

注:

  • 背景がタイルのファッションのように繰り返される必要があるので、私はオプションではありませんストレッチ、ドローアブルを9patch使用することはできません。私はViewの大きさがより小さいときに前に説明したよう
  • 私はViewのための固定の高さを設定することはできません、それは子供の依存(私はViewGroupでこれを使用しています)
  • この奇妙な動作が起こりますビットマップのサイズでなければ、ビットマップは正しくクリップされます(つまり、ビューのサイズがビットマップのサイズの1.5倍であれば、ビットマップの1.5倍になります)。
  • この例では高さを扱いますが、幅は同じです。

答えて

14

getMinimumHeight()およびgetMinimumWidth()から0を返すカスタムBitmapDrawableが必要です。もちろん

import android.content.res.Resources; 
import android.graphics.drawable.BitmapDrawable; 

public class BitmapDrawableNoMinimumSize extends BitmapDrawable { 

    public BitmapDrawableNoMinimumSize(Resources res, int resId) { 
     super(res, ((BitmapDrawable)res.getDrawable(resId)).getBitmap()); 
    } 

    @Override 
    public int getMinimumHeight() { 
     return 0; 
    } 
    @Override 
    public int getMinimumWidth() { 
     return 0; 
    } 
} 

あなたは(私の知る限り)できない、あなたは以下のようなものをのTextViewの背景をインスタンス化して設定する必要がありますので、XMLでカスタムドローアブルを宣言します:ここで私は仕事をしているBitmapDrawableNoMinimumSize名付けまし一つだ

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    BitmapDrawable bmpd =new BitmapDrawableNoMinimumSize(getResources(), R.drawable.tile); 
    bmpd.setTileModeX(TileMode.REPEAT); 
    bmpd.setTileModeY(TileMode.REPEAT); 
    findViewById(R.id.textView).setBackgroundDrawable(bmpd); 
} 

そしてもちろん、あなたはレイアウトXMLからbackground属性を削除します。

<TextView 
    android:id="@+id/textView" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Testing testing testing" 
    android:textColor="#000000" /> 

を私はこれをテストしてみた、動作しているようです。

+0

優秀な回答、ありがとう! – aromero

関連する問題