android - 如何制作这个 Staggered GridView?

标签 android xml android-gridlayout

您好,我试图通过 android studio 中的 xml 代码制作类似的东西,但无法弄清楚虽然我部分到达那里但感觉我没有使用正确的方法有人可以告诉我我如何制作这个布局通过 xml 或 java。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="2dp"
    tools:context="com.stocks.android.gridview.MainActivity">

    <LinearLayout
        android:id="@+id/linear_one"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:orientation="horizontal">

        <android.support.v7.widget.CardView
            android:layout_width="280dp"
            android:layout_height="200dp"
            android:layout_marginRight="5dp"
            android:layout_weight="40"
            app:cardBackgroundColor="#BCE36E"
            app:cardCornerRadius="4dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@drawable/img1" />

        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_weight="60"
            app:cardBackgroundColor="#8BD3FB"
            app:cardCornerRadius="4dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@drawable/img2" />

        </android.support.v7.widget.CardView>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linear_two"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/linear_one"
        android:layout_margin="5dp"
        android:orientation="horizontal">

        <android.support.v7.widget.CardView
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_weight="60"
            app:cardBackgroundColor="#FFB637"
            app:cardCornerRadius="4dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@drawable/img2" />
        </android.support.v7.widget.CardView>

        <LinearLayout
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_marginLeft="5dp"
            android:orientation="vertical">

            <android.support.v7.widget.CardView
                android:layout_width="200dp"
                android:layout_height="100dp"
                app:cardBackgroundColor="#FB7649"
                app:cardCornerRadius="4dp">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:src="@drawable/img3" />

            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:layout_width="200dp"
                android:layout_height="95dp"
                android:layout_marginTop="5dp"
                app:cardBackgroundColor="#F1F1F1"
                app:cardCornerRadius="4dp">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:src="@drawable/img7" />

            </android.support.v7.widget.CardView>
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/linear_two">

        <android.support.v7.widget.CardView
            android:layout_width="280dp"
            android:layout_height="200dp"
            android:layout_marginRight="5dp"
            android:layout_weight="60"
            app:cardBackgroundColor="#F34F45"
            app:cardCornerRadius="4dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@drawable/img6" />

        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_weight="40"
            app:cardBackgroundColor="#55C6FF"
            app:cardCornerRadius="4dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@drawable/img4" />

        </android.support.v7.widget.CardView>
    </LinearLayout>
</RelativeLayout>

最佳答案

为动态高度图像创建一个类。

public class DynamicHeightNetworkImageView extends ImageView {
private float mAspectRatio = 1.5f;

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

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

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

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int measuredWidth = getMeasuredWidth();
    setMeasuredDimension(measuredWidth, (int) (measuredWidth / mAspectRatio));
}

public void setAspectRatio(float aspectRatio) {
    mAspectRatio = aspectRatio;
    requestLayout();
}

并在你的xml文件中使用它

<com.dmitrymalkovich.android.xyzreader.ui.DynamicHeightNetworkImageView
        android:id="@+id/thumbnail"
        android:layout_width="match_parent"
        android:background="@color/material_grey_300"
        android:layout_height="wrap_content" />

查看此 github 代表:https://github.com/DmitryMalkovich/make-your-app-material

关于android - 如何制作这个 Staggered GridView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42876105/

相关文章:

java - 如何在 Android 中以编程方式添加焦点?

android - Facebook key 哈希与第二次运行时任何存储的 key 哈希不匹配

php - SyncML 与 Android 和 PHP Web 服务

android - 创建像 pinterest 应用程序一样的 GridView

java - Android:TableLayout 不显示

android - View.SurfaceView,为什么它的成员mSurfaceHolder从getSurface()返回null?

xml - Oracle - 验证日期格式 (yyyy-mm-ddThh24 :mi:ssZ) in XML against XSD

java - 如何配置嵌入式 Jetty 来拾取网页片段?

android - 使用权重属性动态创建复选框

java - 有什么办法可以从多个网址获取多张图片吗?