1
我試圖做一個HorizontalListView(by DevSmart)
,其中第一項(列)將有Ndp
寬度和所有下一個項目將有N/2dp
寬度。在我的示例N = 300
中。所以我爲標準的Android ListView完美地完成了它,並且它非常棒。但是,按照與HorizontalListView完全相同的邏輯,我面臨着誤解。HorizontalListView縮放內容(項目佈局)
所有項目的寬度和高度與HorizontalListView相同。它 doesen't什麼寬度/高度我指定在
photo.xml
。 而作爲一個結果我得到的物品寬和高大的整個屏幕(因爲我com.example.yura.listviewscale.HorizontalListView
有match_parent
寬度和高度)
有人可以幫助我嗎?
activity_main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.yura.listviewscale.MainActivity" >
<com.example.yura.listviewscale.HorizontalListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/myHzListView" />
</LinearLayout>
photo.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parnet"
android:layout_height="match_parent"
android:id="@+id/photoView" />
</RelativeLayout>
適配器類的存根:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.photo, parent, false);
viewHolder = new ViewHolder();
viewHolder.photoImageView = (ImageView) convertView.findViewById(R.id.photoView);
mInitialWidth = convertView.getLayoutParams().height;
convertView.setTag(viewHolder);
} else
viewHolder = (ViewHolder)convertView.getTag();
Photo photo = mPhotoList.get(position);
if (position != 0) {
RelativeLayout.LayoutParams params = convertView.getLayoutParams();
params.width = (int) (mInitialWidth * 0.5f);
}
viewHolder.photoImageView.setBackgroundDrawable(photo.getDrawable());
return convertView;
}
static final class ViewHolder {
ImageView photoImageView;
}