您可以为不同的方向提供不同的资源。因此,当设备处于横向时,您使用在每个项目ListView
中有两个ImageView
s 的布局填充,而在纵向中,布局ImageView
每个项目有 1 个。
查看提供资源以了解如何为不同的方向提供不同的布局 XML。
示例代码:
// Create an anonymous implementation of OnClickListener
private OnClickListener mImageListener = new OnClickListener() {
public void onClick(View v) {
// do something when the ImageView is clicked
}
};
//Check the orientation and handle accordingly in the getView
public View getView (int position, View convertView, ViewGroup parent){
if(convertView == null){
convertView = (LinearLayout)inflater.inflate(R.layout.list_item, parent);
}
ImageView leftImage = (ImageView)convertView.findViewById(R.id.leftImage);
ImageView rightImage = (ImageView)convertView.findViewById(R.id.rightImage);
boolean isTwoColumn = (rightImage != null);
//If it is two column layout, set both ImageViews
if(isTwoColumn){
leftImage.setImageResource(...);
leftImage.setOnClickListener(mImageListener);
rightImage.setImageResource(...);
rightImage.setOnClickListener(mImageListener);
}else{
leftImage.setImageResource(...);
leftImage.setOnClickListener(mImageListener);
}
}
/res/layout-land/list-item.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="+@id/leftImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="+@id/rightImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
/res/layout-port/list-item.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="+@id/leftImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>