简体   繁体   中英

ImageView in LinearLayout give a fixed width in percent

I'm using a ListView, for each item I created an imageview + an edittext. I load images from a website and I don't understand why some images have not a width of 35%. However I use the layout_weight property. Can you tell me how to force the image to take 35% of the width ?

For example, the result i have now :

在此处输入图片说明

And here is my code :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:weightSum="3"
    >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="0.3"
        android:orientation="horizontal"
        android:padding="5dp" >

        <ImageView
            android:id="@+id/poster"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="6dip"
            android:layout_weight="0.35"
            android:contentDescription="42"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.65"
            android:text="Title"
        />

    </LinearLayout>

</LinearLayout> 

If you see what is wrong :/

Best regards, Zed13

Edit : The result with Rishi Paul solution

在此处输入图片说明

And the updated xml :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:weightSum="3"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="5dp" >

        <ImageView
            android:id="@+id/poster"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="6dip"
            android:layout_weight="0.35"
            android:scaleType="fitCenter"
            android:contentDescription="42"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.65"
            android:text="Title"
        />

    </LinearLayout>

</LinearLayout> 

Do Like This. It Will Help You

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/poster"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginRight="6dip"
    android:layout_weight="0.35"
    android:adjustViewBounds="true"
    android:contentDescription="42"
    android:src="@drawable/desert" />

<TextView
    android:id="@+id/title"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_weight="0.65"
    android:text="Title" />

</LinearLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM