java 以编程方式设置GridView和ImageView的宽高
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25256399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically set the width and height of GridView And ImageView
提问by wizurd
I have seen SO questions regarding how to set the height and width of an ImageView, and a GridView separately. Here, hereand herefor starters
我已经看到了关于如何分别设置 ImageView 和 GridView 的高度和宽度的问题。在这里,这里和这里是初学者
But, I'm wondering how to set both.
但是,我想知道如何设置两者。
Here's my setup. In my GridView, I have an ImageView and a TextView. I have a few images of various widths and sizes, which I want to resize to always be 1/3 the size of the screen square. Underneath, I have a text description.
这是我的设置。在我的 GridView 中,我有一个 ImageView 和一个 TextView。我有一些不同宽度和大小的图像,我想将它们调整为始终是屏幕方块大小的 1/3。在下面,我有一个文字说明。
Here's the code for what I have so far...
这是我到目前为止的代码......
gallerygridview.xml
画廊网格视图.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>
In my Activity...
在我的活动...
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);
}
And in my Adapter class...
在我的 Adapter 类中...
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;
}
For now, the GridView is exactly 1/3rd the size of the screen. And the image and text in both display. But I would like the ImageView to fill the remaining space inside the GridView cell.
目前,GridView 正好是屏幕大小的 1/3。并同时显示图像和文本。但我希望 ImageView 填充 GridView 单元格内的剩余空间。
Here's what I would like...
这是我想要的...
But this is what it looks like...
但这就是它的样子......
What am I doing wrong? And, How do I programmatically set both the height of the GridView, And the ImageView inside it?
我究竟做错了什么?而且,如何以编程方式设置 GridView 的高度和其中的 ImageView?
Thanks in advance.
提前致谢。
回答by Mysterious
Here's a relatively easy method to do this. Throw a GridView into your layout, setting the stretch mode to stretch the column widths, set the spacing to 0 (or whatever you want), and set the number of columns to 2:
这是一个相对简单的方法来做到这一点。将 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>
and refer thisto know more about GridView and ImageView
并参考此以了解有关 GridView 和 ImageView 的更多信息
回答by Uday Sravan K
After seeing your out put images ,here is what I understood and the reason for that problem
看到您的输出图像后,这是我的理解以及该问题的原因
When ever you scale an image ,must be scaled as per aspect ratio of that image. ex: lets say your image dimensions are
900 1200 (w h)
to scale its width to400
need to do like thisnewWidth = 400; newHeight = 1200 * (400/900) = 533.33
If you want image of a imageview should scale automatically then check scaleType properties of ImageView in xml layout(
fitCenter,centerInside
etc)Add this line in your getView
holder.img.setScaleType(ScaleType.CENTER_INSIDE);
当您缩放图像时,必须根据该图像的纵横比进行缩放。例如:假设您的图像尺寸是
900 1200 (w h)
缩放其宽度以400
需要这样做newWidth = 400; newHeight = 1200 * (400/900) = 533.33
如果您希望图像视图的图像应自动缩放,请检查 xml 布局中 ImageView 的 scaleType 属性(
fitCenter,centerInside
等)在您的 getView 中添加这一行
holder.img.setScaleType(ScaleType.CENTER_INSIDE);