2011-07-10 4 views
0

、私はいくつかのヒントの後、私はのために行くことにしましたリニアレイアウトは、右

私のウィジェットに配布したい9x2のPNG年代のグリッドを持ってfill_parent/wrap_contentを取得することはできません(コードをクリーンアップビット)

<linearLayout 
    android:orientation="vertical"> 
    <linearLayout 
    android:orientation="horizontal"> 
     <ImageView /> 
     <ImageView /> 
     <ImageView /> 
     <ImageView /> 
     <ImageView /> 
     <ImageView /> 
     <ImageView /> 
     <ImageView /> 
     <ImageView /> 
    </linearLayout> 
    <linearLayout 
    android:orientation="horizontal"> 
     <ImageView /> 
     <ImageView /> 
     <ImageView /> 
     <ImageView /> 
     <ImageView /> 
     <ImageView /> 
     <ImageView /> 
     <ImageView /> 
     <ImageView /> 
    </linearLayout> 
</linearLayout> 

100万ドルの質問は、要素の幅/高さの設定方法です。オブジェクトがウィジェットの幅全体を埋めるようにしたいので、トップレベルにwidth = "fill_parent"を指定します。しかしそれとは別に、私はちょうどイメージがうまくスケールすることが好きです。私はfill_parent/wrap_contentのコンセプトを忘れてしまったのかと思っています。

私はweightパラメータを使ってexeperimentingを試みましたが、イメージのサイズが異なるため、これはうまくいきませんでした。

上記の要素の幅/高さを定義して、この9x2グリッドが幅を埋めるようにし、高さに必要なだけのスペースを取る方法については、 layout_weight:

おかげ ヨハン

答えて

1

あなたは、レイアウトパラメータのアンドロイドを使用する必要があります。すべての画像ビューのレイアウトがlayout_weight = 1の場合、スペースは均等に分割されます。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/contentContainer" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#FFFFFF"> 

    <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#0000aa"> 
     <ImageView android:layout_weight="1" android:src="@drawable/icon" android:scaleType="fitXY" android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="#ff0000"/> 
     <ImageView android:layout_weight="1" android:src="@drawable/icon" android:scaleType="fitXY" android:layout_width="wrap_content" android:layout_height="fill_parent" /> 
     <ImageView android:layout_weight="1" android:src="@drawable/icon" android:scaleType="fitXY" android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="#ff0000"/> 
     <ImageView android:layout_weight="1" android:src="@drawable/icon" android:scaleType="fitXY" android:layout_width="wrap_content" android:layout_height="fill_parent" /> 
     <ImageView android:layout_weight="1" android:src="@drawable/icon" android:scaleType="fitXY" android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="#ff0000"/> 
    </LinearLayout> 
    <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#00aaaa"> 
     <ImageView android:layout_weight="1" android:src="@drawable/icon" android:scaleType="fitXY" android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="#ff0000"/> 
     <ImageView android:layout_weight="1" android:src="@drawable/icon" android:scaleType="fitXY" android:layout_width="wrap_content" android:layout_height="fill_parent" /> 
     <ImageView android:layout_weight="1" android:src="@drawable/icon" android:scaleType="fitXY" android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="#ff0000"/> 
     <ImageView android:layout_weight="1" android:src="@drawable/icon" android:scaleType="fitXY" android:layout_width="wrap_content" android:layout_height="fill_parent" /> 
     <ImageView android:layout_weight="1" android:src="@drawable/icon" android:scaleType="fitXY" android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="#ff0000"/> 
    </LinearLayout> 
</LinearLayout> 
+0

ありがとうございます!私の問題は、画像の幅が2から80ピクセルまで異なることです。比率を正しく得るために重みをピクセル数に設定することは、私のトリックをしませんでした。 2ピクセル幅のイメージが80ピクセル幅のイメージを占有するのを防ぐにはどうすればよいですか? –

関連する問題