创建基于GridLayout Android的菜单

时间:2013-10-05 01:59:54

标签: android android-layout

我试图实现以下用户界面:包含静态图片和文字的主页。

enter image description here

我首先提出了tablelayout,但是在布局改变的情况下它并不是完全灵活的。然后gridlayout出现了,但它仍然无法与截图完全相同。

所以我的问题是哪种布局最适合?

PS:图像尺寸不同且彼此一致。

2 个答案:

答案 0 :(得分:1)

// try this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:weightSum="2"
              android:orientation="vertical">
    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="vertical">

        <LinearLayout
                android:id="@+id/linearlayout1"
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:layout_height="0dp"
                android:orientation="vertical" >

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:adjustViewBounds="true"
                    android:scaleType="fitXY"
                    android:src="@drawable/ic_launcher"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:padding="5dp"
                android:background="@android:color/transparent"
                android:text="Special"/>
            </RelativeLayout>
        </LinearLayout>

        <LinearLayout
                android:id="@+id/linearlayout1"
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:layout_height="0dp"
                android:orientation="vertical" >

            <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
                <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:adjustViewBounds="true"
                        android:scaleType="fitXY"
                        android:src="@drawable/ic_launcher"/>
                <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentBottom="true"
                        android:padding="5dp"
                        android:background="@android:color/transparent"
                        android:text="Dishes"/>
            </RelativeLayout>
        </LinearLayout>
    </LinearLayout>
    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="vertical">

        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:weightSum="2">
            <LinearLayout
                    android:id="@+id/linearlayout1"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="match_parent"
                    android:orientation="vertical" >

                <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">
                    <ImageView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:adjustViewBounds="true"
                            android:scaleType="fitXY"
                            android:src="@drawable/ic_launcher"/>
                    <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentBottom="true"
                            android:padding="5dp"
                            android:background="@android:color/transparent"
                            android:text="Event"/>
                </RelativeLayout>
            </LinearLayout>

            <LinearLayout
                    android:id="@+id/linearlayout1"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="match_parent"
                    android:orientation="vertical" >

                <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">
                    <ImageView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:adjustViewBounds="true"
                            android:scaleType="fitXY"
                            android:src="@drawable/ic_launcher"/>
                    <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentBottom="true"
                            android:padding="5dp"
                            android:background="@android:color/transparent"
                            android:text="Location"/>
                </RelativeLayout>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

答案 1 :(得分:0)

试试这个

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/linearlayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearlayout2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearlayout1"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearlayout2"
        android:orientation="horizontal"
        android:weightSum="2" >

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>


</RelativeLayout>