【问题标题】:Programmatically set the width and height of GridView And ImageView以编程方式设置GridView和ImageView的宽高
【发布时间】:2014-10-05 01:20:43
【问题描述】:

我已经看到关于如何分别设置 ImageView 和 GridView 的高度和宽度的问题。 Hereherehere 对于初学者来说

但是,我想知道如何设置两者。

这是我的设置。在我的 GridView 中,我有一个 ImageView 和一个 TextView。我有一些不同宽度和大小的图像,我想将其调整为始终为屏幕正方形大小的 1/3。在下面,我有一个文字说明。

这是我目前所拥有的代码......

gallerygridview.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="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/bg_light_blue"
    android:orientation="vertical" >

        <GridView
            android:id="@+id/gridGallery"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:numColumns="3"
            android:padding="0dp"
            android:verticalSpacing="4dp" >
        </GridView>

        <ImageView
            android:id="@+id/imgNoMedia"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:contentDescription="@string/app_name"
            />
        
        <TextView
           android:id="@+id/textView1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:textSize="12sp"
           android:textStyle="bold"
           android:typeface="sans"
           android:layout_below="@+id/imageView1"
           android:layout_marginTop="2dp"
           android:layout_centerHorizontal="true"
           android:textColorHighlight="#000000"
           android:gravity="center"
         />
</LinearLayout>

在我的活动中...

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gallerygridview);
            
     GridView gridView = (GridView) findViewById(R.id.gridGallery);

     m_adapter = new PhotoImageAdapter(getBaseContext(),m_images);
     gridView.setAdapter(m_adapter); // uses the view to get the context instead of getActivity().
     registerForContextMenu(gridView);
     

 }

在我的适配器类中...

public View getView(int position, View convertView, ViewGroup parent) {
 
   View view = convertView;
   Holder holder=new Holder();

         if (view == null) {
             LayoutInflater inflater = LayoutInflater.from(mContext);
                view = inflater.inflate(R.layout.gallerygridview, parent, false);
              

            holder.img = new ImageView(mContext);
            holder.img = (ImageView) view.findViewById(R.id.imgNoMedia);
            holder.tv = (TextView) view.findViewById(R.id.textView1);
            
            
            int iDisplayWidth = mContext.getResources().getDisplayMetrics().widthPixels ;
            int iImageWidth = (iDisplayWidth / 3)-15 ; 
            GridView.LayoutParams glp = (GridView.LayoutParams) view.getLayoutParams();
            glp.width = iImageWidth;
            glp.height = iImageWidth ;
            view.setLayoutParams(glp);
       
            view.setTag(holder);
            
        } else {
            holder = (Holder) view.getTag();
        }

        holder.tv.setText(m_images.get(position).key);
        holder.img.setImageURI(Uri.parse(m_images.get(position).value));
        
    return view;
  }

目前,GridView 正好是屏幕大小的 1/3。并同时显示图像和文本。但我希望 ImageView 填充 GridView 单元格内的剩余空间。

这就是我想要的...

但这就是它的样子……

我做错了什么?还有,如何以编程方式设置 GridView 的高度和其中的 ImageView?

提前致谢。

【问题讨论】:

    标签: java android textview imageview android-gridview


    【解决方案1】:

    这是一个相对简单的方法来做到这一点。将 GridView 放入布局中,设置拉伸模式以拉伸列宽,将间距设置为 0(或任何您想要的),并将列数设置为 2:

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <GridView
            android:id="@+id/gridview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:verticalSpacing="0dp"
            android:horizontalSpacing="0dp"
            android:stretchMode="columnWidth"
            android:numColumns="2" 
            />
    </FrameLayout>
    

    并参考this 了解有关 GridView 和 ImageView 的更多信息

    【讨论】:

    • +1 链接。这正是我想要的。谢谢!
    【解决方案2】:

    看到你的输出图像后,这是我的理解以及问题的原因

    1. 当您缩放图像时,必须根据该图像的纵横比进行缩放。 例如:假设您的图像尺寸为900 1200 (w h) 要将其宽度缩放到400 需要这样做

      newWidth = 400; newHeight = 1200 * (400/900) = 533.33

    2. 如果您希望 imageview 的图像自动缩放,请检查 xml 布局中 ImageView 的 scaleType 属性(fitCenter,centerInside 等)

    3. 在你的 getView 中添加这一行

      holder.img.setScaleType(ScaleType.CENTER_INSIDE);

    【讨论】:

      猜你喜欢
      • 2011-03-09
      • 2011-04-01
      • 1970-01-01
      • 2014-07-11
      • 1970-01-01
      • 1970-01-01
      • 2014-08-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多