dmnlk
dmnlk

Reputation: 3045

How to Adjust Listview Item on Screen Size

I make App which uses Listview

main_acivity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/common_bg_common"
    tools:context=".MainActivity" >
    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="#000000"
        android:dividerHeight="3sp" >
    </ListView>

</RelativeLayout>

and costomItem is below

costomo_cell.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF" >

    <LinearLayout
        android:id="@+id/linear1"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_alignParentTop="true"
        android:background="#cc9933"
        android:orientation="vertical" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical|center_horizontal"
            android:text="dummyCanvas"
            android:textColor="#000000" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linear2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/linear1"
        android:background="#cc3333"
        android:orientation="vertical" >

        <TextView 
            android:id="@+id/_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#000000"/>
    </LinearLayout>

</RelativeLayout>

Now,I want to fill Screen by One Item. I have think to write match_parent in custom_cell.xml,but cant.

I dont want to write xxdp. Linear1 is 2/3 size in screen size. other content (linear2) is 1/3 size...

How I configure Custom cell layout?

--------edited--- I upload scrennshot. black line is one Item separete. I want to adjust on Screen Size capture

Upvotes: 2

Views: 7956

Answers (2)

VinceStyling
VinceStyling

Reputation: 3827

I used setLayoutParams to do that in BaseAdapter.

public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = getLayoutInflater().inflate(R.list_item, null);
        convertView.setLayoutParams(new AbsListView.LayoutParams(
            getListView().getWidth(), AbsListView.LayoutParams.WRAP_CONTENT));
    }
    other code...
}

getListView() is a wrap method, it return a ListView and I use it width as item width, hope could help you.

Upvotes: 2

Prakash Nadar
Prakash Nadar

Reputation: 2711

class MyListItemView extends LinearLayout {

public MyListItemView(Context context) {
    super(context);
    inflate(getContext(), R.layout.costomo_cell, this);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int height = getResources().getDisplayMetrics().heightPixels;
    super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST));
}

Now in the adapter's getView instantiate MyListItemView instance

Upvotes: 3

Related Questions