当前位置: 首页 > 知识库问答 >
问题:

以编程方式设置网格视图和图像视图的宽度和高度

乌修筠
2023-03-14

我见过很多关于如何分别设置ImageView和GridView的高度和宽度的问题。这里,这里,还有这里

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

这是我的设置。在我的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单元格内的剩余空间。

这是我想要的。。。

但这就是它看起来的样子...

我做错了什么?而且,如何以编程方式设置网格视图的高度和其中的图像视图?

提前谢谢。

共有2个答案

东门俊民
2023-03-14

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

>

  • 缩放图像时,必须根据图像的纵横比进行缩放。例如:假设您的图像尺寸为<code>900 1200(宽高)需要这样做

    < code > newWidth = 400新高度= 1200 * (400/900) = 533.33

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

    在getView中添加此行

    < code > holder . img . setscaletype(scale type。CENTER _ INSIDE);

  • 充高扬
    2023-03-14

    这是一个相对简单的方法来做到这一点。将 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>
    

    并参考此内容以了解有关网格视图和图像视图的更多信息

     类似资料:
    • 我想显示 3x3 大小的网格视图。我想根据设备大小设置高度和宽度。我从这个链接中获取参考。 主要活动- 活动_主要- 编辑- 就像首先得到屏幕高度和宽度,然后每个项目的高度和宽度是我得到的屏幕高度和宽度值的1/3。

    • 我想在ImageView上设置动态图像,但覆盖表面视图,并执行一些基本的图像编辑操作,如缩放、旋转和转换。现在,当我在横向模式下使用屏幕旋转旋转ImageView时,它需要竖屏的高度和宽度。我将我的活动锁定在竖屏中。

    • 如何以编程方式设置属性?

    • 问题内容: 如何通过ImageView编程设置的宽度和高度? 问题答案: 设置ImageView的高度可能为时已晚,但是为了其他有相同问题的人: 希望这可以帮助。 重要。如果要在布局已经“布局”之后设置高度,请确保还调用:

    • 我需要使用javaFX2执行以下操作: 1-)加载图像并使用imageView显示。[好的] 2-)右键单击并选择“添加节点”选项,屏幕上将出现一个黑色圆圈,您可以将圆圈拖动到图像的任何位置。[好的] 3-)使用鼠标的滚轮放大或缩小图像视图,在图像上提供“缩放感觉”。[确定] 3.1-)但是,每次缩放图像时,我都希望我的圆圈遵循缩放比例,这意味着它们不能保持在屏幕的相同位置。[不知道] 我的问题是

    • 问题内容: 我正在尝试实施一个。当我使用“自动布局”时,单元格不会更改大小,而是对齐。 现在我想将其大小更改为例如 我尝试设置 虽然没有用。 有没有办法做到这一点? 编辑 : 看来,答案只会改变我的CollectionView宽度和高度本身。约束中可能存在冲突吗?有什么想法吗? 问题答案: 使用此方法设置自定义像元高度宽度。 确保添加此协议 如果您使用的是 swift 5 或 xcode 11 及