我試圖用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);
}
}
我碰到類似的情況。按照您的建議將所有內容轉換爲線性佈局之前,只需要知道,我們是否可以在tablelayout本身修復任何問題? – Srikanth