![](/img/trans.png)
[英]Android Imageview with different sizes while maintaing aspect ratio
[英]Two ImageViews side by side with different image sizes but wanting to maintain same aspect ratio and sizes
這已經困擾了我一段時間了,我不確定如何解決。
假設我具有以下布局:2個ImageView,每個ImageView下都有一個TextView。 正在遠程檢索圖像。 我希望每個ImageView占用它們所在活動的大約40%的寬度,並在其下方留有空白。 TextViews將縮小大約5%,位於每個ImageView下方的中心。
我試過的
將每對ImageView和TextView放置在各自的LinearLayout中,以便放置2個LinearLayouts,並將每個LinearLayout的layout_width設置為fill_parent,並且將二者的layout_weight(0.5)相等。 每個ImageView的layout_width為fill_parent,layout_margin為15dip。 layout_height設置為wrap_content(這是我不確定如何設置的參數)。
效果很好,但問題是所有傳入的圖像都具有不同的大小和比例,因此它們並排看起來很奇怪。
我繼續設置:android:scaleType =“ centerCrop” android:adjustViewBounds =“ true”
這有所幫助,但每個ImageView有時仍具有不同的大小。 在這一點上,我只是將每個ImageView固定在兩個ImageView上的固定寬度為100左右,這很有幫助,但是當然,對於較大尺寸的設備,這看起來很奇怪。 我該如何實現自己想要實現的目標?
在第二個選項上,請在沒有“ AdjustViewBounds”的情況下進行嘗試,請記住:
如果希望ImageView調整其邊界以保留其可繪制對象的寬高比,請將其設置為true。
但是,在您的情況下,這就是我要做的:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="center"
android:src="@drawable/99" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:text="TextView" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="center"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="TextView" />
</LinearLayout>
在較大的屏幕上看起來像:
在較小的屏幕上:
在這種情況下,我更喜歡居中,因為如果您的圖片較小,則它們看起來會更好。 如果可以的話,我會盡量避免使用固定尺寸,因為如您所說,在較大的設備上看起來不太好。
您還可以為每種大小的屏幕進行不同的布局。 但是您將必須保留所有這些。 選中支持多種屏幕尺寸 (如果還沒有;)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.