将网格视图中的子项目居中

时间:2016-03-19 09:03:30

标签: android gridview

我正在尝试使用图像缩略图填充gridview。当图像方向不同,即横向与纵向不同时,我无法将子项目居中并且不匹配。图片展示了它是如何出现的。我不想更改缩略图的大小,而是在同一级别显示文本视图,并相应地居中显示图像视图。

photo gridview with childs not centered

Gridview XML:

OnResultExecuting

ExpandableHeightGridView类:

<syook.syookfirst.classes.Utils.ExpandableHeightGridView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listview_call_photos"
        android:layout_marginLeft="2dp"
        android:layout_marginRight="2dp"
        android:layout_weight="1"
        android:nestedScrollingEnabled="true"
        android:gravity="center"
        android:layout_gravity="center"
        android:numColumns="auto_fit">

    </syook.syookfirst.classes.Utils.ExpandableHeightGridView>

Gridview子布局:

public class ExpandableHeightGridView extends GridView
{

boolean expanded = false;

public ExpandableHeightGridView(Context context)
{
    super(context);
}

public ExpandableHeightGridView(Context context, AttributeSet attrs)
{
    super(context, attrs);
}

public ExpandableHeightGridView(Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
}

public boolean isExpanded()
{
    return expanded;
}

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    // HACK! TAKE THAT ANDROID!
    if (isExpanded())
    {
        // Calculate entire height by providing a very large height hint.
        // But do not use the highest 2 bits of this integer; those are
        // reserved for the MeasureSpec mode.
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);

        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();
    }
    else
    {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

public void setExpanded(boolean expanded)
{
    this.expanded = expanded;
}
}

感谢您的任何帮助。我尝试搜索此类问题,但无法使用提供的任何解决方案解决此问题。

2 个答案:

答案 0 :(得分:1)

将ImageView的高度设置为100dp。谢谢:))

答案 1 :(得分:0)

只需设置固定宽度并将重力设置为中心。 机器人:layout_width =&#34; 8&#34;和  机器人:比重=&#34;中心&#34;