我有一组 12 张图像要在布局中显示。这个数字是半任意选择的,但归结为纵向模式下水平方向为 3 个,垂直方向为 4 个,横向方向则相反。
第一个实现是使用 gridview。问题是不能强制高度适合屏幕。 对于解决方法,我当然可以(尝试)缩放图像,但几乎不可能计算 GridView 可用的空间:当然,总屏幕大小是已知的,但你必须“猜测”通知栏的大小,但事实并非如此。这似乎不是一个优雅的解决方案。测量的尺寸确实不可信:我没有在方向改变时完全重新启动(为了速度),但屏幕构建完整空间的方式在现场不可用。 最后的结论是我不想计算图像的大小然后相应地缩放它们:我认为最好说明 View 应该如何适合屏幕,对吧?
所以接下来的尝试就是使用 TableLayout。使用“ShrinkColumns =”*”图像拟合得很好,所以图像的大小现在是我们想要的。但是我们可能在高度上拥有的“额外”空间现在在表格行之间平均分配。这是可以预料的,但是很丑。
目前的代码似乎无关紧要,因为它不起作用,但最终看起来像这样: 我已经删除了所有的填充和其他看起来不相关的东西。 (肖像:)
<TableLayout
android:shrinkColumns="*">
<TableRow>
<ImageView/>
<ImageView/>
<ImageView/>
</TableRow>
… (repeat 3 tablerows)
</TableLayout>
为了“缩放”过大的图像,TableLayout 具有“shrinkcolumns=”*””属性。
如何让三个 ImageView 在 TableRow 的中心对齐,而不是在宽度上均匀分布?垂直列也是如此,我们如何才能将所有内容放在一起而不是散布在屏幕的高度上? 基本上,“多余”空间应该作为填充/边距放在两侧,现在它位于图像之间。
例子: 左边的屏幕截图显示左/右距离太大,右边的顶部/底部太多
最佳答案
我认为使用自定义 View 应该不会太难,这应该是一个有趣的练习。这是我的第一个自定义 View ;欢迎反馈!
限制
AspectGrid
完全忽略了其子项想要的大小。出于您的目的,这似乎没问题。如果需要更高级的东西,onMeasure
需要做很多额外的工作。AspectGrid
的父级建议的大小被使用时没有再考虑。这与上一期有关。
截图
Landscape screenshot http://tinypic.com/images/404.gif
工作原理
一个主要参数是列数。行数是自动计算的,因为我们知道 child 的数量。另一个主要参数是我们要为 child 使用的纵横比(对于正方形设置为 1)。
在 onLayout
中,我们收到网格的最终大小,因此我们可以计算子项的最大宽度和高度。
然后我们根据宽高比检查它。如果 child 太高,我们就让他们变矮(如肖像示例)。如果它们太宽,我们会让它们变窄(如风景示例)。
仅此而已;剩下的只是管道。
代码
com/photogrid/AspectGrid.java
:实际的ViewGroup
类
package com.photogrid;
import android.content.Context;
public class AspectGrid extends ViewGroup {
private int mNumColumns = 1;
private int mHorizontalSpacing = 0;
private int mVerticalSpacing = 0;
private float mChildAspectRatio = 1.0f;
public AspectGrid(Context context) {
super(context);
}
public AspectGrid(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public AspectGrid(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
try {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AspectGrid);
setNumColumns(a.getInt(R.styleable.AspectGrid_numColumns, mNumColumns));
setHorizontalSpacing(a.getDimensionPixelSize(R.styleable.AspectGrid_horizontalSpacing, mHorizontalSpacing));
setVerticalSpacing(a.getDimensionPixelSize(R.styleable.AspectGrid_verticalSpacing, mVerticalSpacing));
setChildAspectRatio(a.getFloat(R.styleable.AspectGrid_childAspectRatio, mChildAspectRatio));
a.recycle();
} catch (RuntimeException ex) {
throw ex;
}
}
public int getNumColumns() {
return mNumColumns;
}
public void setNumColumns(int numColumns) {
if (numColumns < 1)
throw new IllegalArgumentException("numColumns must be at least 1");
if (numColumns != mNumColumns) {
mNumColumns = numColumns;
requestLayout();
}
}
public int getHorizontalSpacing() {
return mHorizontalSpacing;
}
public void setHorizontalSpacing(int horizontalSpacing) {
mHorizontalSpacing = horizontalSpacing;
}
public int getVerticalSpacing() {
return mVerticalSpacing;
}
public void setVerticalSpacing(int verticalSpacing) {
mVerticalSpacing = verticalSpacing;
}
public float getChildAspectRatio() {
return mChildAspectRatio;
}
public void setChildAspectRatio(float childAspectRatio) {
if (childAspectRatio <= 0)
throw new IllegalArgumentException("childAspectRatio must be positive");
if (childAspectRatio != mChildAspectRatio) {
mChildAspectRatio = childAspectRatio;
requestLayout();
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int measuredWidth = widthSize;
int measuredHeight = heightSize;
int width = Math.max(measuredWidth, getSuggestedMinimumWidth());
int height = Math.max(measuredHeight, getSuggestedMinimumHeight());
setMeasuredDimension(width, height);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childCount = getChildCount();
if (childCount <= 0)
return;
int innerWidth = r - l - getPaddingLeft() - getPaddingRight();
int innerHeight = b - t - getPaddingBottom() - getPaddingTop();
int numRows = (childCount + mNumColumns - 1) / mNumColumns;
int leftEdge = getPaddingLeft();
int topEdge = getPaddingTop();
int horizontalStride = (innerWidth + mHorizontalSpacing) / mNumColumns;
int verticalStride = (innerHeight + mVerticalSpacing) / numRows;
int childWidth = horizontalStride - mHorizontalSpacing;
int childHeight = verticalStride - mVerticalSpacing;
if (childHeight * mChildAspectRatio > childWidth) {
childHeight = (int)(childWidth / mChildAspectRatio);
verticalStride = childHeight + mVerticalSpacing;
topEdge = (innerHeight + mVerticalSpacing - numRows * verticalStride) / 2;
} else {
childWidth = (int)(childHeight * mChildAspectRatio);
horizontalStride = childHeight + mHorizontalSpacing;
leftEdge = (innerWidth + mHorizontalSpacing - mNumColumns * horizontalStride) / 2;
}
for (int i = 0; i < childCount; ++i) {
View child = getChildAt(i);
int row = i / mNumColumns;
int column = i % mNumColumns;
int left = leftEdge + column * horizontalStride;
int top = topEdge + row * verticalStride;
child.layout(
left,
top,
left + childWidth,
top + childHeight);
}
}
}
res/values/attrs.xml
:在 XML 中使用的属性声明
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="AspectGrid">
<attr name="numColumns" format="integer"/>
<attr name="horizontalSpacing" format="dimension"/>
<attr name="verticalSpacing" format="dimension"/>
<attr name="childAspectRatio" format="float"/>
</declare-styleable>
</resources>
res/layout/main.xml
:上面截图中使用的例子
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.photogrid"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.photogrid.AspectGrid
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp"
app:numColumns="3"
app:horizontalSpacing="5dp"
app:verticalSpacing="5dp"
app:childAspectRatio="1.0"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffcccc"
android:text="Item 1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ccffcc"
android:text="Item 2"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ccccff"
android:text="Item 3"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffcc"
android:text="Item 4"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffccff"
android:text="Item 5"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ccffff"
android:text="Item 6"
/>
</com.photogrid.AspectGrid>
</LinearLayout>
关于android - 如何在屏幕中央布局 'grid' 图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4592065/