[英]Android ImageView to expand the whole width of screen
我的xml如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow android:layout_weight="1" android:gravity="center">
<ImageView android:layout_height="200dp"
android:layout_width = "wrap_content"
android:src="@drawable/saltnpepper"
android:scaleType="centerInside" />
</TableRow>
<TableRow android:layout_weight="3" >
<ListView android:id="@+id/list1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
>
</ListView>
</TableRow>
<TableRow android:layout_weight="1" android:gravity="center">
<ImageView android:layout_height="100dp"
android:layout_width = "wrap_content"
android:src="@drawable/halal"
android:scaleType="centerInside"/>
</TableRow>
</TableLayout>
我的屏幕目前是這樣的:
我希望Saltnpepper
圖像在屏幕的寬度上擴展,我已經嘗試將ImageView
的layout_height
設置為0dp
但圖像隨后消失。 我也試過它的layout_width
作為match_parent
,但它沒有用
此外,我想為ListView
添加一些邊距,但如果我將marginRight
或marginLeft
添加到ListView
元素,則整個table_layout
會移動。
在ImageView
match_parent
layout-width設置為match_parent
。 此外,將ImageView
的背景顏色設置為某樣,這樣您就可以看到它實際占用了多少空間。 從那里,您希望使用scaleType
將圖像整形(裁剪/擬合/等)到ImageView的空間。
將圖像視圖寬度設置為match_parent,將height設置為wrap_content,以保持可以使用adjustViewBounds設置為TRUE的寬高比。
使用下面的代碼....並嘗試...
android:layout_width = "match_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:adjustViewBounds="true"
結局代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow android:layout_weight="1" android:gravity="center">
<ImageView android:layout_height="200dp"
android:layout_width = "match_parent"
android:src="@drawable/saltnpepper"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:adjustViewBounds="true"
android:scaleType="centerInside" />
</TableRow>
<TableRow android:layout_weight="3" >
<ListView android:id="@+id/list1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
>
</ListView>
</TableRow>
<TableRow android:layout_weight="1" android:gravity="center">
<ImageView android:layout_height="100dp"
android:layout_width = "wrap_content"
android:src="@drawable/halal"
android:scaleType="centerInside"/>
</TableRow>
</TableLayout>
所以我嘗試了所有的解決方案,但有一件事是有效的,另一件事是扭曲的。 我嘗試了Adeel Turk的解決方案fitXY,它立刻使我的圖像適合屏幕,但圖像太有彈性,所以現在我把我的圖像作為背景,它給了我很好的結果。
這是我的xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/saltnpepper">
<TableRow android:layout_weight="1" android:gravity="center">
<TextView android:layout_height="200dp"/>
</TableRow>
<TableRow android:layout_weight="4" >
<ListView android:id="@+id/list1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
>
</ListView>
</TableRow>
<TableRow android:layout_weight="1" android:gravity="center">
<ImageView android:layout_height="100dp"
android:layout_width = "wrap_content"
android:src="@drawable/halal"
android:scaleType="centerInside"/>
</TableRow>
</TableLayout></LinearLayout>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.