簡體   English   中英

Android ImageView擴展了整個屏幕的寬度

[英]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圖像在屏幕的寬度上擴展,我已經嘗試將ImageViewlayout_height設置為0dp但圖像隨后消失。 我也試過它的layout_width作為match_parent ,但它沒有用

此外,我想為ListView添加一些邊距,但如果我將marginRightmarginLeft添加到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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM