我有一个ListView,我改变了行的外观,但listview的大小是一行,而不是全屏。
list_item.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView_news_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#0082A8" />
<TextView
android:id="@+id/textView_news_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
tab_news.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list_news"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>
制作适配器:
public class RSSAdapter extends ArrayAdapter<RSSitem> {
Context context;
int layoutResourceId;
ArrayList<RSSitem> data = null;
SimpleDateFormat sdf = new SimpleDateFormat("kk:mm ");
public RSSAdapter(Context context, int layoutResourceId, ArrayList<RSSitem> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
NewsHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new NewsHolder();
holder.txtTime = (TextView)row.findViewById(R.id.textView_news_time);
holder.txtNews = (TextView)row.findViewById(R.id.textView_news_header);
row.setTag(holder);
}
else
{
holder = (NewsHolder)row.getTag();
}
RSSitem item = data.get(position);
holder.txtTime.setText(sdf.format(item.getPubDate()));
holder.txtNews.setText(item.getTitle());
return row;
}
static class NewsHolder
{
TextView txtTime;
TextView txtNews;
}
项目正在滚动。
抱歉英语不好。谢谢你的帮助
@Varun的建议没有帮助
我发现了错误。谢谢。回答“答案”)
答案 0 :(得分:91)
我发现了错误。 tab_news位于TabHost的ScrollView中。 如此愚蠢的错误( 谢谢你的帮助。
更新: ListView
不得低于ScrollView
xml 。如果是,则会出现同样的问题。
更新2:您可以使用NestedScrollView
答案 1 :(得分:11)
如果在ScrollView中使用ListView,则会出现此问题。我的解决方案如下:
<强> 1。创建一个不可滚动的自定义列表视图
Declare
TYPE xy_type is table of xy%rowtype index by PLS_Integer;
xyt xy_type;
x number;
y number;
Begin
select * bulk collect into xyt from xy;
x := 1;
y := xyt.count;
for i in x..y loop
insert into xy (x1, y1, z1) values(xyt(i).x1, xyt(i).y1, i);
End Loop;
End;
}
<强> 2。将以上自定义类用于xml文件
public class NonScrollListView extends ListView {
public NonScrollListView(Context context) {
super(context);
}
public NonScrollListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NonScrollListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
对我来说,它适用于所有操作系统版本。 希望最适合你。
答案 2 :(得分:4)
在list_item.xml
的父LinearLayout
中,您正在使用android:layout_height="fill_parent"
显然它会垂直扩展。
OR:
使用monitor
工具,转到hierarchy viewer
,详细检查正在运行的布局。找到有问题的视图,检查其属性/属性是否正确,移动到其父视图并检查它,直到找到问题。
答案 3 :(得分:0)
我认为Listview只会从您的数据中获取一条记录。只需检查您是否已获得所有数据,您希望在Listview中显示这些数据。
OR
请检查list_item.xml,替换
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="**wrap_content**"
android:orientation="vertical" >
代替,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="**fill_parent**"
android:orientation="vertical" >
答案 4 :(得分:0)
动态改变高度大多数时候都会给NPE ...最好的解决方案是创建一个不可滚动的自定义列表视图。你会找到很多代码。