Android ImageView dynamic width and height

元气小坏坏 提交于 2019-12-11 13:14:28

问题


I have a horizontal list of images loaded from a url. So i have an ImageLoader, then an Adapter then my Activity. My xml layouts looks like this:

mainlayout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

  <com.devsmart.android.ui.HorizontalListView
      android:id="@+id/hlist_view" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"
      android:overScrollMode="always" > 
  </com.devsmart.android.ui.HorizontalListView>

</LinearLayout>

Then my list.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" 
android:layout_height="match_parent"
android:orientation="vertical"
android:translationY="200dp" > <!-- make this dynamic -->

    <FrameLayout 
        android:id="@+id/mainlayout" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" >

       <ImageView
        android:id="@+id/prof_pic"    
        android:clickable="true"
        android:adjustViewBounds="true"
        android:layout_width="360dp" 
        android:layout_height="360dp" />

    <ProgressBar
        android:id="@+id/img_progress"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
        </FrameLayout>      

    <TextView
        android:id="@+id/sname"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/sdesc"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

With this layout, the result is Figure A. The gray broken line is the imageView, we can't actually see it but only those images(green box). What i want to achieve is the one in Figure B.

So, to somewhat solve this i tried adding in my imageView

                  <ImageView
                android:id="@+id/prof_pic"    
                android:clickable="true"
                android:adjustViewBounds="true"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:maxWidth="360dp"
                android:maxHeight="360dp"
                android:minHeight="240dp"
                android:minWidth="240dp"
                 />

But what happened is that, by the time i open the application, my image list looks like Figure A. But when a scroll along the images, it adjusted to somewhat look like Figure B. But when i scroll back to the first image, only the first have of the image will only be show or sometimes, my image list starts at my second image. This is really crazy.

Is there any way where i can achieve Figure B without destroying my list of images or at the start of the app, it already displays like Figure B?

EDIT:

This is how my Adapter looks like:

public View getView(int position, View convertView, ViewGroup parent){
    ViewHolder holder = null;

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);



    if(convertView == null){
        convertView = inflater.inflate(R.layout.list_layout, null);
        holder = new ViewHolder();
        holder.txtShopName = (TextView) convertView.findViewById(R.id.shop_name);
        holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
        holder.imageView = (ImageView) convertView.findViewById(R.id.prof_pic);
        holder.progBar = (ProgressBar) convertView.findViewById(R.id.img_progress);


        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }



    imageLoader=new ImageLoader(context.getApplicationContext(), holder.progBar);

    SRowItem sItem = (SRowItem) getItem(position);

    holder.txtName.setText(sItem.getShopName());
    holder.txtDesc.setText(sItem.getShopDesc());
    imageLoader.DisplayImage(sItem.getImageUrl(), holder.imageView);

    return convertView;
}

回答1:


Try something like this :

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);

ImageView imageView = (ImageView) LayoutInflater.from(context).inflate(R.layout.image, null);
imageView.setLayoutParams(params);


来源:https://stackoverflow.com/questions/15781680/android-imageview-dynamic-width-and-height

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!