简体   繁体   English

自定义GridView

[英]Custom GridView

I'm trying to design a Layout wich is like this image : 我正在尝试设计一个像这样的Layout

在此处输入图片说明

I thought that the best way would be using LinearLayout for each item, and I was wrong, because it looks like this : 我认为最好的方法是对每个项目使用LinearLayout ,这是错误的,因为它看起来像这样:

在此处输入图片说明

And the problem is the ImageView that is not in the middle, I tried to put it with margins, but when I try to launch this APP on a Samsung s2 it mess the Layout and on my Lg g3 the ImageView I see on the bottom (without margin) and on s2 I see it on the middle... 问题是ImageView不在中间,我试图将其留在空白处,但是当我尝试在Samsung s2上启动此APP时,它弄乱了Layout ,在我的Lg g3上,我在底部看到的ImageView (没有保证金),在s2上我看到它在中间...

This is my layout.xml 这是我的layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardElevation="5dp"
android:layout_margin="7dp">

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
  <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/LinearImageView">
    <ImageView android:id="@+id/image_holder"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:scaleType="fitXY" />
  </LinearLayout>

    <View
        android:id="@+id/divider"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_below="@id/LinearImageView"
        android:background="@android:color/darker_gray"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/LinearTitulo"
        android:layout_below="@+id/divider"
        android:layout_marginLeft="5dp"
        >
    <TextView
        android:id="@+id/info_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SOME TEXT HERE"
        android:textSize="16sp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/LinearPrice"
        android:layout_marginLeft="5dp"
        android:layout_below="@+id/LinearTitulo">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tvPrice"
            android:text="SOME TEXT HERE"
            android:layout_below="@+id/info_text"/>
    </LinearLayout>

    <ImageView
    android:layout_width="24dp"
    android:layout_height="24dp"
    android:src="@mipmap/ic_launcher"
    android:layout_marginRight="5dp"
        android:layout_alignBottom="@+id/LinearPrice"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />


</RelativeLayout>

I thought that a GridView would be the point, and I saw a library like AsymmetricGridView but it's not what I need because you put it by code (java), and I don't want it. 我以为可以使用GridView ,我看到了一个类似AsymmetricGridView的库,但这不是我所需要的,因为您通过代码(java)放置了它,而我却不想要它。

I suppose that is a GridView with two columns, the first column has 2 rows and the second has one row, but I don't know if it's the best approach, could you guide me? 我想这是一个有两列的GridView ,第一列有2行,第二列有一行,但是我不知道这是否是最好的方法,您能指导我吗?

Here's another layout without fussing with Relative positioning. 这是另一种布局,无需大意相对位置。 I use a similar layout in one of my apps and it works fine, though you'll probably want to season to your own taste ;) 我在其中一个应用程序中使用了类似的布局,并且效果很好,尽管您可能想根据自己的口味进行调味;)

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_vertical" >

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:gravity="center_vertical"
                android:singleLine="true"
                android:textColor="#000000" />

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@android:color/black" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:gravity="center_vertical"
                android:singleLine="true"
                android:textColor="#000000" />

        </LinearLayout>

        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@android:color/black" />

        <ImageView
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_margin="10dp" />

    </LinearLayout>

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

Edit: added pic of output 编辑:添加了输出图片

在此处输入图片说明

Try this: 尝试这个:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="7dp"
    app:cardElevation="5dp">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >

        <LinearLayout
            android:id="@+id/LinearImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/image_holder"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:scaleType="fitXY"/>
        </LinearLayout>

        <View
            android:id="@+id/divider"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_below="@id/LinearImageView"
            android:background="@android:color/darker_gray"/>

        <LinearLayout
            android:id="@+id/bottomLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_below="@+id/divider">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:layout_weight="80">


                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Text 1"
                    android:id="@+id/textView3"
                    android:textSize="40sp"
                    android:paddingLeft="30dp"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Text2"
                    android:id="@+id/textView4"
                    android:textSize="20sp"
                    android:paddingLeft="30dp"/>
            </LinearLayout>

            <ImageView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:src="@drawable/ic_action_armchair"
                android:layout_weight="20"/>

        </LinearLayout>

    </RelativeLayout>

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

渲染图

删除android:layout_alignBottom="@+id/LinearPrice"并将android:layout_centerVertical="true"到您的ImageView

How about this as a reference: 如何作为参考:

在此处输入图片说明

   <?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="wrap_content"
    android:orientation="horizontal"
    android:background="#111">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:textColor="#fff"
            android:textSize="18sp"
            android:text="TextView1"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#fff"
            android:padding="10dp"
            android:textSize="18sp"
            android:text="TextView2"/>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="right">
        <ImageView
            android:layout_width="50sp"
            android:layout_height="50sp"
            android:layout_gravity="center"
            android:background="@drawable/placeholder_loader"
            />
    </LinearLayout>




</LinearLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM