繁体   English   中英

在列表视图内设置相对布局高度

[英]Set Relativelayout height inside listview

我想在ListView.设置一个relativeLayout高度ListView.

这是我在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);
}

我正在添加xml以获得我的代码的更多信息

<?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"/>

但我收到一条错误消息,指出"E/AndroidRuntime(11406): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams"

有人可以告诉我我的代码有什么问题吗? 谢谢

工作正常,使用AbsListView代替RelativeLayout。 谢谢你Piyush Kukadiya先生

尝试这个 :

更改

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));  

更新:

RelativeLayout.LayoutParamsAbsListView.LayoutParamsandroid.widget包的两个不同类。

在这里,您将使用更通用的ViewGroup.LayoutParamsRelativeLayout.LayoutParams替换正确类型为AbsListView.LayoutParams现有布局参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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