【问题标题】:ImageView doesn't fit with my linearlayout heightImageView 不适合我的线性布局高度
【发布时间】:2016-06-06 10:13:15
【问题描述】:

我的 linearLayout (weightSum=10) 中有 5 个图像视图(layout_weight=2 每个) 所有的图像视图都有 0dp 宽度和 wrap_content 高度,但我总是有一个顶部和底部边距...... 当我使用设计视图时,如果我尝试手动调整我的线性布局高度(通过扩展边框),最后一个被设置为固定值,这不是我想要的原因(不再动态)

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/fond">

    <TextView/>

    <LinearLayout>

        <ImageView/>

        <TextView/>

        <TextView/>

        <ImageView/>

    </LinearLayout>

    <TextView
        android:text="ECRANS DISPONIBLES"
        android:drawableLeft="@drawable/selectsite_picto_search"
        android:drawablePadding="2dip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        android:textStyle="normal"
        android:layout_margin="10dp"
        android:id="@+id/homeScreenAvailableTextView"
        android:textColor="#FFF" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:background="#26FFFFFF"
        android:weightSum="10">

        <ImageView
            android:id="@+id/item_home_screen1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:src="@drawable/home_poster1"
            android:layout_weight="2"/>

        <ImageView
            android:id="@+id/item_home_screen2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:src="@drawable/home_poster2"
            android:layout_weight="2"/>

        <ImageView
            android:id="@+id/item_home_screen3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:src="@drawable/home_poster3"
            android:layout_weight="2"/>

        <ImageView
            android:id="@+id/item_home_screen4"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:src="@drawable/home_poster4"
            android:layout_weight="2"/>

        <ImageView
            android:id="@+id/item_home_screen5"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:src="@drawable/home_poster5"
            android:layout_weight="2"/>

    </LinearLayout>

    <TextView/>

    <TextView/>

    <TextView/>

    <TextView/>
</LinearLayout>

</ScrollView>

【问题讨论】:

  • 看我的回答@gamerounet..!!

标签: android imageview android-linearlayout android-layout-weight


【解决方案1】:

我终于找到了解决办法 android:scaleType="fitCenter"android:adjustViewBounds="true" 在每个imageView中,并将layout_height="wrap_content" 放到linearLayout中

    <TextView
        android:text="ECRANS DISPONIBLES"
        android:drawableLeft="@drawable/selectsite_picto_near"
        android:drawablePadding="2dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="12sp"
        android:textStyle="normal"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="20dp"
        android:id="@+id/homeScreenAvailableTextView"
        android:textColor="#FFF" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="#26FFFFFF"
        android:weightSum="10">
  •     <ImageView
            android:id="@+id/item_home_screen2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:src="@drawable/home_poster2"
            android:layout_weight="2"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true" />
    
        <ImageView
            android:id="@+id/item_home_screen3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:src="@drawable/home_poster3"
            android:layout_weight="2"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true" />
    
        <ImageView
            android:id="@+id/item_home_screen4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:src="@drawable/home_poster4"
            android:layout_weight="2"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true" />
    
        <ImageView
            android:id="@+id/item_home_screen5"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:src="@drawable/home_poster5"
            android:layout_weight="2"
            android:scaleType="fitStart"
            android:adjustViewBounds="true" />
    
    </LinearLayout>
    

【讨论】:

    【解决方案2】:
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="#26FFFFFF"
        android:weightSum="10">
    
        <ImageView
            android:id="@+id/item_home_screen1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:src="#AAAAAA"
            android:layout_weight="2"/>
    
        <ImageView
            android:id="@+id/item_home_screen2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:src="#BBBB00"
            android:layout_weight="2"/>
    
        <ImageView
            android:id="@+id/item_home_screen3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:src="#CC00CC"
            android:layout_weight="2"/>
    
        <ImageView
            android:id="@+id/item_home_screen4"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:src="#DDBDDD"
            android:layout_weight="2"/>
    
        <ImageView
            android:id="@+id/item_home_screen5"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:src="#00DDDD"
            android:layout_weight="2"/>
    
    </LinearLayout>
    

    //试试这个

    【讨论】:

    • 没有变化,还是一样的结果。您只能通过 match_parent 修改 wrap_content 吗?
    【解决方案3】:

    请为所有图像视图提供android:scaleType="fitXY"。这会将图像视图的高度设置为与线性布局的高度相同。

    【讨论】:

    • 确实 IV 的高度与线性布局相同,但我的图像变形/拉伸
    猜你喜欢
    • 1970-01-01
    • 2021-05-03
    • 2020-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-07
    • 2020-01-26
    相关资源
    最近更新 更多