【问题标题】:GridLayout(Not GridView) : How to create Items With different Heights through xmlGridLayout(Not GridView) : 如何通过xml创建不同高度的项目
【发布时间】:2014-07-03 09:33:46
【问题描述】:

我有这样的视图片段

在这个网格布局中,我的项目必须像这样排列

你可以看到物品有不同的高度。甚至每行之间的空间也不同。我尝试通过提供行跨度来实现这一点。但我所有的逻辑都错了

我参考这些链接:Link 1,Link 2,Link 3

但是我的想法对我没有帮助。谁能建议我如何实现这一目标

编辑:接受答案后

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

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="0.35">

</LinearLayout>

<GridLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="horizontal"
    android:rowCount="20"
    android:columnCount="1">

    <LinearLayout
        android:layout_row="0"
        android:layout_gravity="fill">
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="Name"
            android:gravity="left"
            android:layout_weight="1"/>

        <EditText
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="EditText"
            android:gravity="center"
            android:layout_weight="1"/>
        <Space 
            android:layout_width="45dp"
            android:layout_height="match_parent"/>
    </LinearLayout>
    <Space
        android:layout_row="1"
        android:layout_gravity="fill"
        android:layout_rowSpan="1"/>
    <LinearLayout
        android:layout_row="3"
        android:layout_gravity="fill"
        android:layout_rowSpan="4">
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="Address"
            android:gravity="left"
            android:layout_weight="1"/>

        <EditText
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:lines="4"
            android:text="EditText"
            android:gravity="center"
            android:layout_weight="1"
           />

        <Space 
            android:layout_width="45dp"
            android:layout_height="match_parent"/>
    </LinearLayout>
    <Space
        android:layout_row="7"
        android:layout_gravity="fill"/>
    <LinearLayout
        android:layout_row="8"
        android:layout_gravity="fill">
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="Email"
            android:gravity="left"
            android:layout_weight="1"/>


        <EditText
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="EditText"
            android:gravity="center"
            android:layout_weight="1"/>
        <ImageView android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:paddingLeft="30dp"
            android:src="@android:drawable/arrow_up_float"/>

    </LinearLayout>
    <Space
        android:layout_row="9"
        android:layout_gravity="fill"/>
    <LinearLayout
        android:layout_row="10"
        android:layout_gravity="fill"
        android:layout_rowSpan="6">
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="About Me"
            android:gravity="left"
            android:layout_weight="1"/>

        <EditText
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:lines="5"
            android:text="EditText"
            android:gravity="center"
            android:layout_weight="1"/>
        <Space 
            android:layout_width="45dp"
            android:layout_height="match_parent"/>
    </LinearLayout>
    <Space
        android:layout_row="16"
        android:layout_gravity="fill"
        android:layout_rowSpan="3"/>
    <LinearLayout
        android:layout_row="19"
        android:layout_gravity="fill">
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="Nick Name"
            android:gravity="left"
            android:layout_weight="1"/>

        <EditText
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="EditText"
            android:gravity="center"
            android:layout_weight="1"/>
      <ImageView android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:paddingLeft="30dp"
            android:src="@android:drawable/arrow_up_float"/>
    </LinearLayout>

</GridLayout>

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="0.30">

</LinearLayout>

【问题讨论】:

  • 你能用两个或三个布局来处理吗?如果是,那么您需要 getItemViewTypegetViewTypeCount 来创建它
  • @shayanpourvatan 你在说 gridView
  • 是的,抱歉我没看到GridLayout,链接3没有帮助到你?这有什么问题?
  • @shayanpourvatan 我尝试使用 rowspan 项目没有展开,我也尝试更改 layout_gravity。但没用
  • 你能发布你的xml吗?

标签: android android-layout android-view android-gridlayout


【解决方案1】:
// Try this way,hope this will help solve your problem...

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

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.35">

    </LinearLayout>

    <GridLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:rowCount="20"
        android:columnCount="1">

        <LinearLayout
            android:layout_row="0"
            android:layout_gravity="fill">
            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:text="Name"
                android:gravity="left"
                android:layout_weight="1"/>

            <EditText
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:text="EditText"
                android:gravity="center"
                android:layout_weight="1"/>
        </LinearLayout>
        <Space
            android:layout_row="1"
            android:layout_gravity="fill"
            android:layout_rowSpan="1"/>
        <LinearLayout
            android:layout_row="3"
            android:layout_gravity="fill"
            android:layout_rowSpan="4">
            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:text="Address"
                android:gravity="left"
                android:layout_weight="1"/>

            <EditText
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:lines="4"
                android:text="EditText"
                android:gravity="center"
                android:layout_weight="1"/>
        </LinearLayout>
        <Space
            android:layout_row="7"
            android:layout_gravity="fill"/>
        <LinearLayout
            android:layout_row="8"
            android:layout_gravity="fill">
            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:text="Email"
                android:gravity="left"
                android:layout_weight="1"/>

            <EditText
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:text="EditText"
                android:gravity="center"
                android:layout_weight="1"/>
        </LinearLayout>
        <Space
            android:layout_row="9"
            android:layout_gravity="fill"/>
        <LinearLayout
            android:layout_row="10"
            android:layout_gravity="fill"
            android:layout_rowSpan="6">
            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:text="About Me"
                android:gravity="left"
                android:layout_weight="1"/>

            <EditText
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:lines="5"
                android:text="EditText"
                android:gravity="center"
                android:layout_weight="1"/>
        </LinearLayout>
        <Space
            android:layout_row="16"
            android:layout_gravity="fill"
            android:layout_rowSpan="3"/>
        <LinearLayout
            android:layout_row="19"
            android:layout_gravity="fill">
            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:text="Nick Name"
                android:gravity="left"
                android:layout_weight="1"/>

            <EditText
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:text="EditText"
                android:gravity="center"
                android:layout_weight="1"/>
        </LinearLayout>

    </GridLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.30">

    </LinearLayout>

</LinearLayout>

【讨论】:

  • 感谢您的努力。你能给我更多帮助吗?我需要在电子邮件和昵称文本视图之后添加一个图像视图。我实现了我将发布 xml。可以看看,告诉我有没有更好的方法来实现这一目标??
  • 你需要把它放在 Nick Name 和 Email EditText 或 TextView 之后吗?
  • 那么为什么你给这些图片左填充以及为什么在不需要图片的地方使用空格?
  • 我想平行对齐编辑文本,编辑文本和图像之间必须有一个空格,我为图像视图提供填充。所以为了实现编辑文本的类似视图,我使用不需要图像的空间
【解决方案2】:

//你需要做wieghtsum

<?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:layout_margin="20dp"
    android:orientation="horizontal"

    android:weightSum="125" >

    <View
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="35" />

    <GridLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="65" />

    <View
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="30" />

</LinearLayout>

【讨论】:

  • 实际上在第二张图片的网格布局中需要帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多