0

Je veux définir une hauteur relativeLayout dans un ListView.Régler la hauteur de l'intérieur Relativelayout listview

Ceci est mon code dans MenuListAdapter.java

RelativeLayout background = (RelativeLayout) convertView.findViewById(R.id.background_color); 
ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon); 
TextView txtTitle = (TextView) convertView.findViewById(R.id.title); 
txtTitle.setTypeface(typeface); 
if(navDrawerItems.get(position).getType() == MenuType.HEADER) 
{ 
    background.setBackgroundColor(context.getResources().getColor(R.color.header_color)); 
    background.setClickable(false); 
    background.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT)); 
}else{ 
    background.setBackgroundColor(color.transparent); 
} 

J'ajoute xml pour plus d'informations de mon code

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:background="@android:color/transparent" 
    android:layout_height="wrap_content" 
    android:id="@+id/background_color"> 

<ImageView 
    android:id="@+id/icon" 
    android:layout_width="@dimen/spacing_layout_image" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_marginLeft="@dimen/spacing_margin_text" 
    android:layout_marginRight="@dimen/spacing_margin_text" 
    android:src="@drawable/menu_btn" 
    android:layout_centerVertical="true" /> 

<TextView 
    android:id="@+id/title" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@id/icon" 
    android:minHeight="?android:attr/listPreferredItemHeightSmall" 
    android:textAppearance="?android:attr/textAppearanceListItemSmall" 
    android:gravity="center_vertical" 
    android:textColor="@drawable/menu_text" 
    android:paddingRight="@dimen/spacing_scrollview" 
    android:layout_centerVertical="true"/> 

<TextView 
    android:id="@+id/notifTotal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginRight="@dimen/spacing_margin_text" 
    android:background="@android:color/holo_red_dark" 
    android:layout_centerVertical="true" 
    android:layout_alignParentRight="true" 
    android:textColor="@android:color/white" 
    android:paddingLeft="@dimen/radius_default" 
    android:paddingRight="@dimen/radius_default" 
    android:visibility="gone"/> 

mais j'obtiens une erreur sayin Est-ce que quelqu'un peut me dire ce qui ne va pas avec mon code? "E/AndroidRuntime(11406): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams"

Quelqu'un peut-il me dire ce qui ne va pas avec mon code? Merci

Cela fonctionne, utilisez AbsListView au lieu de RelativeLayout. Merci pour Mr.Piyush Kukadiya

+0

pouvez-vous poster du code XML ?? –

Répondre

0

Essayez ceci:

changement

background.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT)); 

à

background.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT)); 

MISE À JOUR:

RelativeLayout.LayoutParams et AbsListView.LayoutParams sont deux classes différentes de android.widget Package.

Ici, vous remplacez les paramètres de disposition existants du type correct AbsListView.LayoutParams avec ViewGroup.LayoutParams plus générique, c'est-à-dire RelativeLayout.LayoutParams.

+0

Merci monsieur piyush. Ça marche. Pouvez-vous m'expliquer pourquoi dois-je utiliser AbsListView au lieu de RelativeLayout? –

+0

Alors, c'est le problème? Merci encore une fois M. Piyush. Je voudrais upvote mais comme c'est un nouveau compte. Je dois atteindre 15 réputation. –

Questions connexes