Android的ImageView的动态宽度和高度 [英] Android ImageView dynamic width and height

查看:140
本文介绍了Android的ImageView的动态宽度和高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个URL加载图像的水平列表。所以我有一个ImageLoader的,那么适配器然后我的活动。我的XML布局看起来是这样的:

mainlayout.xml

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=横向>

  < com.devsmart.android.ui.Horizo​​ntalListView
      机器人:ID =@ + ID / hlist_view
      机器人:layout_width =match_parent
      机器人:layout_height =match_parent
      机器人:overScrollMode =总是>
  < /com.devsmart.android.ui.Horizo​​ntalListView>

< / LinearLayout中>
 

然后我的 list.xml

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直
机器人:translationY =200dp> <! - 让这个充满活力 - >

    <的FrameLayout
        机器人:ID =@ + ID / mainlayout
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_width =WRAP_CONTENT>

       < ImageView的
        机器人:ID =@ + ID / prof_pic
        机器人:可点击=真
        机器人:adjustViewBounds =真
        机器人:layout_width =360dp
        机器人:layout_height =360dp/>

    <进度
        机器人:ID =@ + ID / img_progress
        风格=机器人:ATTR / progressBarStyleLarge
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT/>
        < /的FrameLayout>

    <的TextView
        机器人:ID =@ + ID / SNAME
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:TEXTSTYLE =黑体/>

    <的TextView
        机器人:ID =@ + ID / SDESC
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT/>

< / LinearLayout中>
 

通过这种布局,其结果是图A.灰色的虚线是ImageView的,我们不能真正看到它,但只有那些图像(绿框)。我想要实现的是一个如图B

所以,有些解决这个我尝试添加在我的ImageView

 < ImageView的
                机器人:ID =@ + ID / prof_pic
                机器人:可点击=真
                机器人:adjustViewBounds =真
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:=了maxWidth360dp
                机器人:=了maxHeight360dp
                机器人:=了minHeight240dp
                机器人:=了minWidth240dp
                 />
 

但是,什么情况是,到时候我打开应用程序,我的图片列表看起来像图A.但​​是,当沿图像的滚动,它调整到有点像图B.但是,当我滚动回第一图像,只有第一有图像的将只显示或有时,我的图像列表开始于我的第二图像。这真是疯了。

有什么办法,我可以实现图B,不破坏我的图片,或者在应用程序的启动列表中,它已经显示如图B?

编辑:

这是我的适配器是这样的:

 公开查看getView(INT位置,查看convertView,ViewGroup中父){
    ViewHolder支架=无效;

    LayoutInflater充气=(LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);



    如果(convertView == NULL){
        convertView = inflater.inflate(R.layout.list_layout,NULL);
        持有人=新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 =(进度)convertView.findViewById(R.id.img_progress);


        convertView.setTag(保持器);
    }其他{
        支架=(ViewHolder)convertView.getTag();
    }



    ImageLoader的=新ImageLoader的(context.getApplicationContext(),holder.progBar);

    SRowItem SITEM =(SRowItem)的getItem(位置);

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

    返回convertView;
}
 

解决方案

尝试是这样的:

  LinearLayout.LayoutParams PARAMS =新LinearLayout.LayoutParams(宽,高);

ImageView的ImageView的=(ImageView的)LayoutInflater.from(上下文).inflate(R.layout.image,NULL);
imageView.setLayoutParams(PARAMS);
 

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;
}

解决方案

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);

这篇关于Android的ImageView的动态宽度和高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆