Trần Hoà
Trần Hoà

Reputation: 115

How to set width to ImageView in ListView for mutil screen

I have a ListView with an ImageView and TextView, I want to set the width of the image as 2/3 of the screen.

How do I create a custom ImageView to do this? This is what I've tried so far.

Custom View

public class CustomImageListView extends ImageView {
    public CustomImageListView(Context context) {
        super(context);
    }

    public CustomImageListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomImageListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        // Snap
        // to
        int width = MainActivity.width;
        int height = MainActivity.height;
        int w = width *2/5;
        int h = w *4/6;
        setMeasuredDimension(w, h);
        // setMeasuredDimension(ChoseImage.width, ChoseImage.height); // Snap to
        // //
        // width
    }

}

The custom View XML

<tuannt.tinmoi.custom.CustomImageListView
            android:id="@+id/ivAvater"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/nomage" />

Upvotes: 1

Views: 78

Answers (2)

Evin1_
Evin1_

Reputation: 12906

You don't need to create a Custom View to achieve this. You can make use of the layout_weight attribute.

This would be your layout for every item in the ListView.

item_in_listview.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

</LinearLayout>

The layout_weight attribute is explained in this question.

In a nutshell, layout_weight specifies how much of the extra space in the layout to be allocated to the View.

Upvotes: 0

Alok Gupta
Alok Gupta

Reputation: 1360

You can define a horizontal linear layout with WeightSum = 3 and inside it put another layout with with layout_weight = 1, the remaining portion will be of weight = 2, so you can put your imageView there. Refer below:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/fullscreen"
    style="@style/translucent"
    android:orientation="horizontal"
    android:weightSum="3">

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_gravity="right"
        android:gravity="center"
        android:background="#88000000"
        android:id="@+id/sidebar"
        android:layout_weight="1" >

    </RelativeLayout>

<!-- other views, with a total layout_weight of 2 -->

</LinearLayout>

Upvotes: 1

Related Questions