2011-08-07 41 views
0

我試圖用10個相等高度的行進行佈局,每行包含一個ImageView和一個TextView。當我將圖像放入ImageView中時,我希望它縮放以保留相同的外觀並適合ImageView,而不會更改ImageView大小或包含TableRow的高度。ImageView在顯示圖像時展開

我遇到的問題是,當我將圖像加載到ImageView中時,ImageView似乎展開,並且它也改變了TableRow(?)的高度,我一直在看這個,但可以似乎沒有得到它的工作。這可能是我錯過了Android佈局或ImageView的工作原理之一(?)

我從我的代碼中創建了一個展示問題的小例子。我還在ImageView上設置了背景色,以便更容易看到它改變大小。

下面是我的佈局文件和代碼(道歉的格式,我也把整個Eclipse項目在http://dl.dropbox.com/u/28937795/ImageRowSample.zip

任何幫助,將不勝感激。 =)

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文檔說

TableLayout的子項無法指定layout_width屬性。寬度始終爲 MATCH_PARENT。但是,layout_height屬性可以由孩子定義;默認值 是WRAP_CONTENT。如果孩子是一個TableRow,那麼高度總是WRAP_CONTENT。

該文檔的TableRow也說

通過tablerow的孩子不需要指定layout_width和XML文件中layout_height 屬性。 TableRow總是強制這些值分別爲 MATCH_PARENT和WRAP_CONTENT。

所以,是的,問題是TableRows總是忽略我的設置並使其高度WRAP_CONTENT,然後也迫使ImageView的高度爲WRAP_CONTENT。當我用正確的方向將TableLayout和TableRows轉換爲LinearLayouts時,一切都按我原先的預期工作......

+0

我碰到類似的情況。按照您的建議將所有內容轉換爲線性佈局之前,只需要知道,我們是否可以在tablelayout本身修復任何問題? – Srikanth