2011-08-07 20 views
0

イメージビューとTextViewを含む10行の等高線を持つレイアウトを作成しようとしています。 ImageViewにイメージを置くと、同じアスペクトを保持し、ImageView内に収まるようにスケーリングされ、ImageViewのサイズや含まれるTableRowの高さは変更されません。イメージを表示するときにImageViewが展開される

私が持っている問題は、私はImageViewのにイメージをロードすると、ImageViewのが拡大しているようだ、それはまたのTableRowの高さ(?)私はしばらくの間、これを見てきたが変更されますが、できることですそれが働くように見える。 AndroidレイアウトやImageViewがどのように動作するかについての詳細を見逃している可能性がありますか?

問題を示すコードから小さな例を作成しました。また、ImageViewで背景色を設定して、サイズを変更するのが簡単になるようにしました。

以下は私のレイアウトファイルとコードされている任意の助けをいただければ幸いです

(フォーマットについて謝罪、私はまた、http://dl.dropbox.com/u/28937795/ImageRowSample.zipで完全なEclipseプロジェクトを入れています)。 =)

MAIN.XML 

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
> 



<TableRow style="@style/SlashRowStyle"> 
    <ImageView style="@style/SlashViewStyle" /> 
    <TextView style="@style/NumberViewStyle" android:text="0" /> 
</TableRow> 

<TableRow style="@style/SlashRowStyle"> 
    <ImageView style="@style/SlashViewStyle" /> 
    <TextView style="@style/NumberViewStyle" android:text="1" /> 
</TableRow> 

<TableRow style="@style/SlashRowStyle"> 
    <!-- I'm setting the background of this ImageView 
       so it's easier to see it change size --> 
    <ImageView android:id="@+id/image2" style="@style/SlashViewStyle" 
       android:background="#FF0000"/> 
    <TextView style="@style/NumberViewStyle" android:text="2" /> 
</TableRow> 

<TableRow style="@style/SlashRowStyle"> 
    <ImageView style="@style/SlashViewStyle" /> 
    <TextView style="@style/NumberViewStyle" android:text="3" /> 
</TableRow> 

<TableRow style="@style/SlashRowStyle"> 
    <ImageView style="@style/SlashViewStyle" /> 
    <TextView style="@style/NumberViewStyle" android:text="4" /> 
</TableRow> 

<TableRow style="@style/SlashRowStyle"> 
    <ImageView style="@style/SlashViewStyle" /> 
    <TextView style="@style/NumberViewStyle" android:text="5" /> 
</TableRow> 

<TableRow style="@style/SlashRowStyle"> 
    <ImageView style="@style/SlashViewStyle" /> 
    <TextView style="@style/NumberViewStyle" android:text="6" /> 
</TableRow> 

<TableRow style="@style/SlashRowStyle"> 
    <ImageView style="@style/SlashViewStyle" /> 
    <TextView style="@style/NumberViewStyle" android:text="7" /> 
</TableRow> 

<TableRow style="@style/SlashRowStyle"> 
    <ImageView style="@style/SlashViewStyle" /> 
    <TextView style="@style/NumberViewStyle" android:text="8" /> 
</TableRow> 

<TableRow style="@style/SlashRowStyle"> 
    <ImageView style="@style/SlashViewStyle" /> 
    <TextView style="@style/NumberViewStyle" android:text="9" /> 
</TableRow> 


<LinearLayout style="@style/ButtonBarStyle" > 
    <Button android:text="Add Image" android:onClick="onClickedAddImage" 
      android:gravity="center" android:layout_width="0dip" 
      android:layout_height="fill_parent" android:layout_weight="1" /> 

    <Button android:text="Clear Image" android:onClick="onClickedClearImage" 
      android:gravity="center" android:layout_width="0dip" 
      android:layout_height="fill_parent" android:layout_weight="1" /> 
</LinearLayout> 

</TableLayout> 

STYLES.XML 
<?xml version="1.0" encoding="utf-8"?> 
<resources> 

<style name="SlashRowStyle" > 
    <item name="android:layout_width">fill_parent</item> 
    <item name="android:layout_height">0dip</item> 
    <item name="android:layout_weight">1</item> 
    <item name="android:gravity">center</item> 
</style> 

<style name="SlashViewStyle" > 
    <!-- I want scaling so it fits in the Imageview with the same aspect. 
      I tried both fitCenter and centerInside --> 
    <item name="android:scaleType">fitCenter</item> 
    <item name="android:layout_width">0dip</item> 
    <item name="android:layout_height">fill_parent</item> 
    <item name="android:layout_weight">40</item> 
    <item name="android:gravity">center</item>  
</style> 

<style name="NumberViewStyle"> 
    <item name="android:layout_width">0dip</item> 
    <item name="android:layout_height">fill_parent</item> 
    <item name="android:layout_weight">60</item> 
    <item name="android:gravity">center</item>  

    <item name="android:typeface">sans</item> 
    <item name="android:textStyle">bold</item> 
    <item name="android:textSize">20sp</item> 
    <item name="android:textColor">#0000FF</item> 
</style> 


<style name="ButtonBarStyle"> 
    <item name="android:layout_width">fill_parent</item> 
    <item name="android:layout_height">wrap_content</item> 
    <item name="android:orientation">horizontal</item> 
    <item name="android:background">#404040</item> 
    <item name="android:paddingTop">5dip</item> 
</style> 

</resources> 

IMAGEROWSAMPLEACTIVITY.JAVA 

public class ImageRowSampleActivity extends Activity { 

private ImageView _iv; 

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

    _iv = (ImageView)findViewById(R.id.image2); 
} 



public void onClickedAddImage(View v) { 
    _iv.setImageResource(R.drawable.x); 
} 



public void onClickedClearImage(View v) { 
    //_iv.setImageResource(0);  //Tried this also 
    _iv.setImageDrawable(null); 
} 

} 

答えて

1

私はついにそれを見て、答えに遭遇しました。

問題は、TableLayoutのTableRowsです。 Android SDKのTableLayout docによると、

の子はlayout_width属性を指定できません。幅は常に MATCH_PARENTです。ただし、layout_height属性は子によって定義できます。デフォルト値 はWRAP_CONTENTです。子がTableRowの場合、高さは常にWRAP_CONTENTです。

のTableRowドキュメントものTableRowの

子供がlayout_widthを指定する必要があり、XMLファイルで 属性をlayout_heightていないと言います。 TableRowは常にこれらの値をそれぞれ MATCH_PARENTとWRAP_CONTENTに強制します。

だから、ええ、問題はその後もWRAP_CONTENTするImageViewのの高さを強制的に、TableRowsは常に自分の設定を無視して、その高さWRAP_CONTENTを作っていました。私がTableLayoutとTableRowsを正しい向きでLinearLayoutsに変換すると、元々期待通りにすべてが機能しました...

+0

私は同様の状況に遭遇しました。あなたが示唆したようにすべてを線形レイアウトにする前に、テーブルレイアウト自体で修正できるものはありますか? – Srikanth