gridview布局中心(android)

时间:2015-07-12 18:42:57

标签: android xml gridview

抱歉我的英文。我希望在中心布局中找到gridview。但我有这个:

enter image description here

我使用此属性android:layout_gravity="center_horizontal"android:gravity="center"但是没有帮助

<LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
   <RelativeLayout
                    android:layout_gravity="center_horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <service.ExpandableHeightGridView
                        android:layout_gravity="center_horizontal"
                        android:id="@+id/gridView"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="1dp"
                        android:columnWidth="150dp"
                        android:drawSelectorOnTop="true"
                        android:numColumns="auto_fit"
                        android:stretchMode="columnWidth"
                        android:verticalSpacing="1dp"
                        android:focusable="true"
                        android:clickable="true"/>

                </RelativeLayout>
 </LinearLayout>

ExpandableHeightGridView

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.
            // View.MEASURED_SIZE_MASK represents the largest height possible.
            int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
                    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;
    }
}

项目gridview

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingTop="1dp"
    android:paddingLeft="1dp"
    >

    <ImageView
        android:id="@+id/imageRoom"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:scaleType="centerCrop" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18dp"
        android:text="Large Text"
        android:textColor="#fff"
        android:id="@+id/textView30"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />


</RelativeLayout>

0 个答案:

没有答案