[英]ImageView and Listview - Android
美好的一天!!!
我在android的横向布局中存在布局问题。
当我要将方向更改为横向时,似乎ImageView处于固定位置,并且我只能在列表视图中看到滚动条...我只想有一个布局,也可以滚动imageview以便我可以清楚地看到列表项。
这是我的布局XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:layout_height="wrap_content"
android:contentDescription="@string/app_name"
android:src="@drawable/banner" />
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/imageView1"
android:drawSelectorOnTop="true" />
</RelativeLayout>
这有点棘手,因为您不能将listview放在scrollview内,但是如果您创建一个可以具有不同类型的行的自定义适配器,则可以使其工作(例如,仅包含文本的行,具有ImageView的行等) )。
然后,使该ListView中的第一行始终为图像。
查阅本教程 (部分:不同列表项的布局)以了解如何制作不同类型的行。
我认为在ScrollView中使用ListView不是一件好事,如果您希望该图像也可以滚动而不是这样:
1)对于Listview,您需要一个custome 适配器 ,其中在定制适配器类的getview()方法中,您必须执行类似以下操作
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = getLayoutInflater();
View row;
row = inflater.inflate(R.layout.listview_rowlayout, parent, false);
TextView textview = (TextView) row.findViewById(R.id.tv_list);
ImageView imageview = (ImageView) row.findViewById(R.id.iv_list);
textview.setText(data_text[position]);
if(position==0){
imageview.setImageResource(R.drawable.YourimageName);
}else{
imageview.setVisibility(View.GONE);
}
return (row);
}
您的listview_rowlayout.xml是这样的
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical" >
<ImageView
android:id="@+id/iv_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParenttop="true"
android:layout_marginLeft="10dp"
/>
<TextView
android:id="@+id/tv_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textColor="#000000"
android:layout_alignBottom="@+id/imageView1"
android:text="" />
</RelativeLayout>
您在其中添加了listview的main.xml应该是这样的
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="true" />
</RelativeLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.